Home

The Ch Language Environment Embedded Ch SDK User's Guide

image

Contents

1. printf func arg num for arg 4 of f7 is d n Ch FuncArgFuncArgNum printf func arg num for arg 1 of f8 is d n Ch FuncArgFuncArgNum printf func arg num for arg 2 of f8 is d n Ch FuncArgFuncArgNum printf func arg num for arg 3 of f8 is d n Ch FuncArgFuncArgNum printf data size for arg 1 of fl is d n Ch FuncArgUserDefin printf data size for arg 4 of fl is d n Ch FuncArgUserDefin printf data size for arg 1 of f6 is d n Ch FuncArgUserDefin printf data size for arg 2 of f6 is d n Ch FuncArgUserDefin printf data size for arg 3 of f6 is dMn Ch FuncArgUserDefin printf data size for arg 4 of f6 is d n Ch FuncArgUserDefin printf data size for arg 5 of f6 is d n Ch FuncArgUserDefin printf data size for arg 6 of f6 is dWMn Ch FuncArgUserDefin printf data size for arg 7 of f6 is dMn Ch FuncArgUserDefin printf data size for arg 8 of f6 is d n Ch FuncArgUserDefin printf data size for arg 9 of f6 is d n Ch FuncArgUserDefin printf data name for arg 1 of fl is p n Ch FuncArgUserDefin printf data name for arg 4 of fl is p n Ch FuncArgUserDefin printf data name for arg 1 of f6 is s n Ch FuncArgUserDefin printf data name for arg 2 of f6 is s n Ch FuncArgUserDefin printf data name for arg 3 of f6 is s n Ch FuncArgUserDefin printf data name for arg 4 of f6 is s n Ch FuncArgUserDefin printf data name for arg 5 of f6 is s n Ch Fun
2. fassume CHHOME is C Ch If not change C Ch to CHHOME such as D Ch CC cl INC IC Ch extern include CFLAG MD LFLAG C Ch extern lib chsdk lib X C Ch extern lib embedch lib advapi32 lib target embedchl ex mbedchlb ex mbedch2 exe reopen exe X mbedch exe chhom x mbedchprograms exe parseprograms exe thread exe threadswin exe append ex mbedch ex mbedch c CC embedch c INC LFLAG CFLAG o embedch ex mbedchl ex mbedchl c CC embedchl c INC LFLAG CFLAG o embedchl ex mbedchlb ex mbedchlb c CC embedchlb c INC S LFLAG CFLAG o embedchlb ex mbedch2 ex mbedch2 c CC embedch2 c INC LFLAG CFLAG o embedch2 ex reopen ex reopen c CC reopen c INC LFLAG CFLAG o reopen exe chhome exe chhome c CC chhome c INC S LFLAG CFLAG o chhome exe embedchprograms exe embedchprograms c CC embedchprograms c INC S LFLAG CFLAG o embedchprograms exe parseprograms exe parseprograms c CC parseprograms c INC S LFLAG CFLAG o parseprograms exe thread exe thread c CC thread c INC LFLAG CFLAG o thread exe threadswin exe threadswin c CC threadswin c INC S LFLAG CFLAG o threadswin exe append exe append c CC append c INC S LFLAG CFLAG o append exe clean rm f o obj exe dl lib exp Program 1 3 Makefile in Windows for all examples in this chapter CHAPTER 1 EMBEDDING C
3. Ch VaEnd interp ap return chexpreval c A Ch function file with global variables and functions BRK OK KR KR RR Ck k OR Ck k Ck k OR kA OK Test the function Ch_ExprEval in C space FER KK RR AA Ck Ck Ck k Ck k Ck k I OR ke x A x include lt dlfcn h gt void chexpreval void dlhandle fptr dlhandle dlopen libch dl RTLD LAZY if dlhandle NULL printf Error s dlopen s n __func__ dlerror return fptr dlsym dlhandle chexpreval chdl if fptr NULL printf Error s dlsym s n func dlerror return dlrunfun fptr NULL chexpreval if dlclose dlhandle 0 printf Error s dlclose s n __func__ dlerror return return KR KKK A KG A X e X e x A x application FORK ke e e ee ee eee x x fj int ii 10 double dd 1 1 void fun printf in fun in expreval ch ii d dd f n ii dd 252 Ch ExprEval Chapter A APIs for Embedding Ch lt embedch h gt Ch ExprEval chexpreval Output ii 10 dd 1 100000 in fun in expreval ch ii 10 dd 1 100000 After dd dd ii ii 10 dd 11 100000 in fun in expreval ch ii 10 dd 11 100000 See Also Ch SymbolAddrByName Ch DataType Ch CallFuncByAddr Ch_CallFuncByName Ch ExprCalc Ch ExprParse Ch ExprValue Ch DeleteExpr Value 253 Chapter A APIs for Embedding Ch
4. remove expr return 0 7 4 A SAMPLE DEBUGGER Debug Menu Ok k ck ck ck ck ck RR kk kk KK KK koe kN m gt load filename for debugging in start the program with debugging Wn run the program without debugging Nn step into a function or next line n step over a function or next line n continue till hitting a breakpoint or ends Min change stack to the calling function Wn change stack to the called function Nn display stack names in all stacks Nn display variables and values within its scope n display variables and values in all stacks Mn add an expression into the watch list Wn remove an expression from the watch list Mn remove all expressions from the watch list n set a new breakpoint in a file at line 4n set a new breakpoint in a function Mn set a new breakpoint for a controlling variable n clear a breakpoint in a file at line 4n clear a breakpoint for a function Mn clear a breakpoint for a variable Wn clear all breakpoints n display this debug menu n assign a value to a variable n call a function Wn print out the value of an expression print out the value of an expression n abort the debugger in Program 7 15 The user interface for the debug program menu c 178 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM File Name include lt stdio h gt
5. create dgptr gt argvv from args for Ch_ParseScript dgptr gt argvv 0 dgptr gt progname getArgvFromStr dgptr argvv args dgptr gt runmode runmode Ch Initialize amp interp dgptr gt option dgptr gt interp interp mask CH MASKLINE CH MASKRET Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr clientdata dgptr count status Ch ParseScript interp dgptr gt argvv if status CH OK status Ch ExecScript interp dgptr gt argvv 0 return Status Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 180 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chRun debugInfo t dgptr char args int mask status ChInterp t interp if dgptr gt progname NULL printErrorNoFile return 1 create dgptr gt argvv from args for Ch_ParseScript dgptr gt argvv 0 dgptr gt progname getArgvFromStr dgptr argvv args dgptr gt runmode RUNMODE RUN mask CH MASKNONE if dgptr gt interp NULL already start by chStart status Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr gt clientdata dgptr count else Ch Initialize amp interp dgptr gt option dgptr gt interp interp Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr gt clientdata dgptr count status Ch ParseScript interp d
6. d 500 0 use scanf to obtain the value from the user setVariable interp g amp d use scanf or fgets to obtain an expression strcpy expr i y exprCalculation interp strcpy expr 2 i r g exprCalculation interp printf Mn if Ch_ChangeStack interp expr expr level amp calldata2 CH OK printCalldata amp calldata2 setVariable interp x 4j setVariable interp r amp d use scanf or fgets to obtain an expression stropy expr i x exprCalculation interp strcpy expr 2 i r g exprCalculation interp expr expr 162 Program 7 11 C program for obtaining information for variables of user defined type prog5 c CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES int setVariable ChInterp t interp ChType t datatype int retval datatype Ch DataT switch datatype const char name ype interp name case CH INTTYPE retval Ch SetVar interp name datatype break case CH DOUBLETYPE retval Ch SetVar interp name datatype break default printf datatype d not supported datatype Ch Home interp break return retval int exprCalculation ChI ChType_t datatype int retval nterp_t interp Tn a double d if Ch_ExprParse interp expr CH ERROR return 1 datatype Ch_D
7. Pointer t dgptr trlen proghome char ste char args UM ARGS sizeof char and RegOueryValuel 7 4 A SAMPLE DEBUGGER no buffer for debugging purpose no buffer for debugging purpose modify code here to setup dgptr gt interp from an environment variable for proghome from a registry value in Windows Ex strlen embedch 1 Program 7 16 Functions corresponding to the debug user interface menu debug c 179 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chCleanDebug debugInfo t dgptr int i 1 watchExpression t wexpr head if dgptr gt progname free dgptr gt progname while dgptr gt argvv i NULL free dgptr gt argvv i i if dgptr gt option if dgptr option chhome free dgptr option chhome free dgptr gt option clean the list of watched expressions and breakpoints chRemoveWatchExprs dgptr chClearBreakPoints dgptr return 0 int chLoadFile debugInfo_t dgptr char progname if access progname F OK access progname R OK printf Error file s does not exist or not readable n progname return 1 dgptr gt progname strdup progname return 0 int chStart debugInfo t dgptr runmode t runmode char args int mask status ChInterp t interp if dgptr gt progname NULL printErrorNoFile return 1
8. int main this line will not trigger the callback this line will not trigger the callback int x this line will not trigger the callback int n 10 initialization triggers the callback int a n declaration of VLA triggers the callback this line will not trigger the callback x 10 assignment statement triggers the callback this line will not trigger the callback return 0 return statement triggers the callback this line will not trigger the callback To trigger lines including non executable openning block and closing block statements set the mask as follows int event CH_MASKLINE CH_MASKBLOCK CH_MASKEND 211 Chapter A APIs for Embedding Ch lt embedch h gt Ch AddCallback If the Ch script does not contain function main the callback will be invoked when it is triggered by the first callback event If the Ch script contains function main the callback will be enabled only when the function main beginning its execution Therefore the included header files by a C program will not invoke callbacks When the function atexit in Ch is executed the callback is disabled to avoid execution of the cleanup functions in Ch A callback function can be disabled by setting the event to Ch MASKNONE A callback function of type ChCallback t has the following function prototype void callback ChInterp t interp ChBlock t c
9. ChBlock t The details for each member of the structure ChBlock t are described on page The events that trigger the callback can be obtained inside a callback by event masks using the bit wise and operator amp For example the code fragment below can determine if the callback was triggered by a call event void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata callback triggered when Ch Ch calls and enters a new function if calldata event amp CH MASKCALL printf event MASKCALL is true n The last argument clientdata of a callback function is a generic pointer It points to the client data in the fourth argument of Ch AddCallback when the callback was added When the event contains the event mask Ch MASKCOUNT the last argument count of Ch AddCallback specifies that the callback function will be called after Ch executes every count instruc tions The member count of calldata of structure ChBlock t in the callback gives the number of count instructions after Ch executes to trigger the callback function The count argument of Ch AddCallback and member of structure ChBlock t is meaningful only when the even mask includes Ch MASKCOUNT As an example Ch Program 7 1 first calls function main which in turn calls function funcl There is a block for an if statement inside function funcl Program 7 2 will execute the script in Program 7 1 using Embedded Ch A callback function
10. If the conditional expression for a breakpoint is NULL or the conditional value is true or has changed this function returns 0 Otherwise it returns 1 int chTestBreakPointCondition ChInterp t interp breakPoint t bp char expr cptr void exprptr ChType t datatype size t size int i retval 1 xpr bp condexpr if expr NULL return 0 if interp NULL Ch ExprParse interp expr CH ERROR return 1 datatype Ch DataType interp expr Size Ch DataSize interp expr exprptr void malloc size if Ch ExprCalc interp expr datatype exprptr CH ERROR return 1 if bp condvalue NULL first time bp gt condvalue exprptr else if bp gt condtrue for i 0 cptr exprptr i lt size i if cptr N0 not equal 0 retval 0 break else if memcmp bp gt condvalue exprptr size memcpy bp gt condvalue exprptr size save the value for the expression retval 0 free exprptr return retval Program 7 17 The callback for the debug program callback c Contd 198 Appendix A APIs for Embedding Ch lt embedch h gt A 1 Information in Header File embedch h The header embedch h defines macros and APIs for Embedded Ch Data Types The following data type are defined in the header file embedch h Data Type ChBlock t ChCallback t ChFile t ChFuncType t Ch
11. atexit dlclose sample in C space else include ch h EXPORTCH extern double funcl double x EXPORTCH extern double func2 double x double y endif endif SAMPLE H Program 5 24 C Ch header file sample h target prog xe prog exe prog o func o ch dllink prog exe embedded prog o func o prog o prog c ch dlcomp libfunc dl prog c func o func c ch dlcomp libfunc dl func c clean rm o dl exe Program 5 25 Makefile to create prog exe for Unix Makefile exe 98 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE target libsample dl libsample dl sample chdl o ch dllink libsample dl sample chdl o sample chdl o sample chdl c ch dlcomp libsample dl sample chdl c I clean rm o dl exe Program 5 26 Makefile to create libsample dl for Unix Makefile dl target prog exe prog exe prog obj func obj ch dllink prog exe embedded prog obj func obj prog obj prog c ch dlcomp libfunc dl prog c func obj func c ch dlcomp libfunc dl func c clean del obj exp lib dl exe Program 5 27 Makefile to create prog exe for Windows Makefile exe target libsample dl libsample dl sample chdl obj ch dllink libsample dl embedded sample chdl obj prog lib sample chdl obj sample chdl c ch dlcomp libsample dl sample chdl c I clean del obj exp lib dl exe Progr
12. ii2 amp i2 pass the address to the Ch space assign the address of i2 to the address of the temporary variable i2 in Ch so that the variable in C can be accessed from Ch space by its address If the variable i2 in C is changed the one in Ch will change correspondingly The function changeCVar chdl changes the values of i1 and i2 in C space The embedded Ch script shown in Program 4 9 prints out the corresponding values of i1 and i2 in Ch space before and after changeCVar is called The i2 is changed from 200 to 400 whereas i1 keeps the original value 100 The result from executing embedded Ch Program 4 9 within the application Program 4 5 is appended at the end of the file Note that the function changeCVar in Program 4 9 calls the interface function changeCVar chdl Program 4 4 to access and change the values of the variables in C space 71 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 2 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE ifndef EXPVAR H define EXPVAR H int i1 TAE ERZ define i2 i2_ load DLL include lt dlfcn h gt void _ChExpvar_handle ifndef _HPUX_ _ChExpvar_handle dlopen NULL RTLD LAZY if ChExpvar handle NULL fprintf stderr Error s dlopen s n _ func dlerror fprintf stderr cannot get ChExpvar handle in expvar h Mn exit 1 release DLL when program exits void dlclose expvar void
13. 312 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedMemlInfoB yIndex the value for dimis 2 extent 0 is 3 and extent 1 is 4 Fields isbitfield fieldsize and fieldoffset are for handling members of bit fields If the member is a bit field isbitfield is 1 Otherwise it is 0 Fields fieldsize and fieldoffset give the field size and offset of the field respectively For example for member j struct tag int 1535 int k 3 int j 10 fieldsizeis 10and fieldoffset is 6 Field udtag gives the tag of a user defined structure class or union type for the member if the member is a user defined type of CH STRUCTTYPE CH CLASSTYPE or CH UNIONTYPE For example for members s and sp in the structure tag1 struct tagl struct tag2 s struct tag3 sp by the values for udtag gives the tag for structures tag2 and structures tag3 respectively This field can be used to obtain the information about the user defined type and its members If the input argument udt ag is NULL or memname is not a member name of the user defined type the function returns CH ERROR the member dt ype of the input argument meminfo is assigned with the value of CH UNDEFINETYPE This value can alos be used to test if the memname is a valid member name or the function call is successful Example See Ch_DataType Programs 2 9 and See Also Ch_UserDefinedInfo Ch_UserDefinedMemInfoByName Ch_UserDefinedTag Ch_DataT
14. d n ch_data 0 ch_data 9 ch_data 0 ch_data 9 Program 2 7 Ch script with values assigned in C space setvar ch ch_i 10 ch_i 10 ch_p 20 c_i 30 ch_data 9 10 in the Ch function chfun ch_data 0 1 ch_data 9 10 ch_data 9 11 Figure 2 4 Output from executing setvar2 c 36 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS struct tag int i double d int p int a 2 3 struct tag s 10 20 NULL 30 sp sa 2 10 20 NULL 30 Cnt db int main s p amp i sp 68s printf amp s Sp n amp s printf amp s i Sp n S amp s i printf amp sp pMn amp sp printf amp sp i Sp n amp sp i return 0 Program 2 8 Ch program with user defined data type userdefinedsymbol ch 2 4 Evaluating Expressions Ch provides API Ch ExprEval to evaluate expressions in a Ch program This function prototype is given in header file embedch h as follows int Ch ExprEval ChInterp t interp char expr The first argument of this function is an instance of Ch interpreter The second argument is the string of the expression to be evaluated in Ch space The function returns zero on successful execution non zero on failure C program expreval c shown in Program 2 10 is an example of evaluating expressions in a Ch program The Ch program executed by embedded Ch is shown in Program 2 11 Two variables ii wit
15. double b 2 3 1 2 3 4 5 6 ptrd 3 int n 2 r double vl n double v2 n m double complex z complex 3 4 char strl 2 stringl char str2 10 string2 char str3 string3 char str4 string41 string42 NULL struct tag int i double d s 10 20 struct tag sp amp S struct tag sa 2 10 20 30 40 enum tage red green blue 1 blue string t str5 abcd long long 11 100 char c A bool bo true ptri amp i ptrd b v1 0 1 v1 1 2 v2 0 0 1 v2 1 2 6 void funcl int i double g 200 int y printf funcl is called n EE int i 20 z i 30 printf argument i in funcl is true n int func2 int i printf func2 is called n return 2 i int main int i 10 x ftunceltiry return 0 Program 7 10 Ch program with user defined data type prog4 ch 156 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES funcl is called argument i in funcl is true event MASKEND is true The stack level is 0 The current line number is 39 Function funcl starts at line 30 Function funcl ends at line 40 The function name is funcl The current file name is prog4 ch Function funcl at stack level 0 has 6 symbols d 30 z 0 func funcl g 200 0000 y 0 i 10 The stack level is 1 The current line number is 50 Function main start
16. lt embedch h gt Ch ExprParse Ch ExprParse Synopsis include lt ch h gt int Ch_ExprParse ChInterp_t interp const char expr Purpose Parse an expression in Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter expr Expression in Ch space to be parsed Description The function Ch ExprParse parses an expression in Ch space If the expression is syntactically correct it can be evaluated by function Ch ExprEval The expression expr may invoke functions located in function files specified by the function file path fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is an operand of an expression Example See Ch ExprParse See Also Ch SymbolAddrByName Ch DataTypeO Ch CallFuncByAddr Ch_CallFuncByName Ch ExprEvalO Ch ExprValue Ch DeleteExpr Value 254 Chapter A APIs for Embedding Ch lt embedch h gt Ch ExprValue Ch Expr Value Synopsis include lt ch h gt ChValueNode t Ch_ExprValue ChInterp_t interp const char expr void result Purpose Obtain the value of an expression in the Ch space Return Value This function returns the value node of an expression in the Ch space on success and NULL on failure Parameters interp A Ch interpreter expr The expression
17. CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 7 DISTRIBUTING EMBEDDED CH IN YOUR APPLICATIONS include lt stdio h gt int main printf hello good n printf hello missing quoteWMn return 0 Program 1 13 Script with an error for testing stderr redirection Note that you can capture the error messages from Ch Embedded Ch API s by redirecting them using Ch Reopen except for the error message from the API Ch Initialize In this case you can create a console application based on one of demos in the directory CHHOME toolkit demos embedch guide Then you should see the error message from Ch Initialize 1 7 Distributing Embedded Ch in Your Applications If the second argument of function call ChInitialize is NULL such as Ch Initialize interp NULL The CHHOME toolkit embedchis used as the home directory of Embedded Ch This is the case for the development of applications with Embedded Ch In this case the startup file CHHOME toolkit embedch config chrc will be executed in this initialization function Embed ded Ch searches for the dynamically loaded libraries such as chmt 1 d1 and chmt2 d1 in the directory CHHOME toolkit embedch To distribute your application with Embedded Ch after obtaining the authorization the Embedded Ch located in CHHOME toolkit embedch can be bundled with your application program for distribution This directory contains the Ch run time environment Typically it will be p
18. Ch AppendParseScriptFile interp filename other API such as Ch CallFuncByName to access variables and functions in script code and filename Ch End interp 18 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS e while 1 Ch Initialize amp interp option Ch RunScript interp argv other API such as Ch CallFuncByName to access variables and functions in script argv 0 Ch End interp e while 1 Ch Initialize amp interp option Ch ParseScript interp argv Ch ExecScript interp argv 0 other API such as Ch CallFuncByName to access variables and functions in script argv 0 Ch End interp In an application with multi threads each thread can have a separate instance of Ch interpreter In this case multiple Ch programs can be executed simultaneously by multiple Ch interpreters embedded in the application Each Ch interpreter maintains its own namespace and can also be used to process multiple Ch programs as shown in Program I 8 The maximum number of active Ch interpreters which do not include interpreters terminated by Ch End is decided by the number of dynamically loaded embedded Ch library chmt dl in the Windows system directory in Windows and in the directory usr 1ib in Unix Program I TOJillustrates how to invoke multiple Ch interpreters embedded in a multi thread application using POSIX pthread In this example th
19. Purpose Obtain the number of elements of a local or global variable of array type in the Ch space if it is array type Return Value If the local or global variable is of array type This function returns the number of elements of the array Otherwise it returns 0 Parameters interp A Ch interpreter name Name of the variable or expression in the Ch space Description This function returns the number of elements of a local or global variable in the Ch space The dimension of the array can be obtained by function Ch_ArrayDim Example See Ch_DataType See Also Ch_DataType Ch ArrayDim Ch ArrayExtent Ch_ArrayType Ch_FuncType Ch_FuncArgNum Ch_IsFuncVarArg Ch_UserDefinedTag Ch_UserDefinedInfo 223 Chapter A APIs for Embedding Ch lt embedch h gt Ch ArrayType Ch ArrayType Synopsis include lt ch h gt ChType_t Ch_ArrayType ChInterp_t interp const char name Purpose Determine if a local or global variable in the Ch space is an array and its array type Return Value Based on its argument this function returns one of the macros below defined in header file ch h Macro Description Example CH_UNDEFINETYPE not an array int i CH_CARRAYTYPE C array int a 3 CH_CARRAYVLATYPE C VLA array int a n int func int a n int b int c amp CH CHARRAYTYPE Ch array array int a 3 CH CHARRAYPTRTYPE pointerto Ch array array int ap 3 CH CHARRAYVLATYPE Ch VLA array array int a n
20. if status CH ERROR printf Error Ch ParseScript failed n exit 1 j sy printf calling the callback Nn status Ch CallFuncByName interp callback NULL printlist list printf after calling the callback n if status CH ERROR printf Error Ch CallFuncByName failedin exit 1 status Ch CallFuncByName interp callback NULL otherFunc list Ch End interp void printlist char list int i printf list in printlist in C is p n list for i 0 list i h i printf list 5d Ss n i list i this function is called from the Ch space EXPORTCH void callback chdl void varg ChInterp t interp ChVaList t ap void callback char char list Ch VaStart interp ap varg callback Ch VaArg interp ap FUNCPTR list Ch VaArg interp ap char printf list in callback chdl in C is p n list now we call the real function callback list Ch VaEnd interp ap 96 Program 5 22 A C program calling back a C function from a Ch function without a function file prog4 c CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 4 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE If applications programs such as prog exe are created using other automation process such as inte grated development environment compile option DWINDOWS DLINUX DLINUXPPC DSOLARIS DHPUX DDARWIN
21. tag called case 2 Ch_InitGlobalVar interp 0 tag tag called tag member2 called tag tag called tag memberl called g 10 case 3 Ch_InitGlobalVar interp 1 tag tag called tag memberl called g 10 tag member2 called tag tag called See Also Ch_InitGlobal Var Ch_ParseScript Ch_RunScript Ch AppendParseScript Ch AppendRunScript 285 Chapter A APIs for Embedding Ch lt embedch h gt Ch Initialize Ch Initialize Synopsis include lt embedch h gt int Ch Initialize ChInterp t interpp ChOptions t option Purpose Initialize embedded Ch for executing Ch programs from C space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter option Options for setting embedded Ch ChOpt ions t is defined as a struct as the following typedef struct ChOptions int shelltype shell type char chhome Embedded Ch home directory ChOptions t Field shelltype indicates the type of the Ch shell It could be CH REGULARCH for a regular Ch shell or CH SAFECH for a safe Ch shell By default its value is ChOptions t The field chhome contains the home directory for the Embedded Ch In most applications the Embedded Ch home directory is different from that for a standalone Ch In Windows the Windows system dynamically linked library ch d11 for for standalone Ch is installed in directory To avoi
22. 10 20 30 40 class tagc c union tagu u float func return 0 0 float funcv int i 233 Chapter A APIs for Embedding Ch lt embedch h gt float funcvptr int i array int b 2 3 1 2 3 4 5 6 b2 n 2 n array double arrayfunc void 3 array double a 3 return a array double arrayfunc2 int i int j 1 int main Sp amp S Output n is an int type Ch_DataSize interp n 4 d is double type a is a value of int ais a Cvarra Array dim of a is 1 Array elements of a is 2 Extent of the 1st dim of a 2 ap 10 Ch DataSize interp a 8 a2 is a value of int a2 is a C VLA array Array dim of a2 is 1 Array elements of a2 is 10 Extent of the 1st dim of a2 10 Ch DataSize interp a2 40 pa is not an array pa is a pointer to int ap func return type is float func 3 0 is double type func is a function Number of arguments of function func is 0 funcv is a function prototype Number of arguments of function funcv is 1 funcv is a function of variable argument number funcvptr is a pointer to function 0 Number of arguments of function funcvptr is 1 funcvptr is a function of variable argument number udinfo dtype CH STRUCTTYPE 1 struct size of s is 16 struct name of s is tag the number of members of s is 2 meminfo index 1 meminfo memname f meminfo offset 8 me
23. Ch VarType Synopsis include lt ch h gt ChVarType t Ch VarType ChInterp t interp const char name Purpose Determine if a symbol is a variable in the Ch space Return Value If the argument is not a variable this function returns CH NOTVARTYPE with the value of O If the argument is a variable this function returns a non zero value of ChVarType t The data type ChVarType_t defined inside the header file embedch h has the following values Value Description CH NOTVARTYPE not a variable CH GLOBALVARTYPE a global variable CH LOCALVARTYPE a local variable Parameters interp Ch interpreter name Name of the variable in the Ch space Description Function Ch SymbolTotalNum count all symbols including variables tag names for classes structures and unions as well as names defined by typedef The function Ch VarType deter mines if a symbol is a variable global or local variable For symbols tag tage red type t in struct tag int i enum tag2 red blue green typedef int type t function Ch VarType returns CH NOTVARTYPEwhich is defined as 0 If the function Ch VarType is called when a Ch program runs in a function or block scope the argument name is a local variable it returns CH LOCALVARTYPE If the argument name is a global variable it returns CH GLOBALVARTYPE To check a variable of the global scope when a Ch program is running in a function or block the scope resolution operator can be
24. Chapter A APls for Embedding Ch lt embedch h gt Ch DataType include lt limits h gt include lt embedch h gt struct tag int i double f l int main ChInterp t interp int status char argvv varinfo ch NULL int ap bp struct tag sp sp2 ChUserDefinedTag t udtag ChUserDefinedInfo t udinfo ChMemInfo t meminfo Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program varinfo ch failed n Q re if Ch DataType interp n CH INTTYP printf n is an int type n printf Ch_DataSize interp n dWMn Ch DataSize interp n if Ch_DataType interp d CH DOUBLETYPE printf d is double type n if Ch_DataType interp a CH INTTYPE printf a is a value of int n if Ch_ArrayType interp a CH CARRAYTYPE printf a is a C array n printf Array dim of a is dWMn Ch_ArrayDim interp a printf Array elements of a is d n Ch ArrayNum interp a printf Extent of the 1st dim of a dWMn Ch ArrayExtent interp a 0 ap int Ch SymbolAddrByName interp a printf ap d n ap printf Ch_DataSize interp a dWMn Ch DataSize interp a if Ch_DataType interp a2 CH INTTYPE printf a2 is a value of int n if Ch_ArrayType interp a2 CH CARRAYV
25. Classl and member function Class1 memfunl in Program 6 36 each calls its corresponding chdl function using dlrun fun Then the chdl file shown in Program S 31 in turn calls the binary C member functions The output of executable compiled from Program 5 32 is shown in Figure 5 4 105 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE File script cpp include sampclass h int main class Classl cl class Classl c2 new Classl class Classl c3 2 cl memfunl 100 c2 memfunl1 200 c3 0 memfun1 300 c3 1 memfunl 300 delete c2 Program 5 35 Ch Program script cpp 106 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE member functions of Classl Classi Class1 void fptr fptr dlsym g sample dlhandle Classl Classl chdl if fptr NULL printf Error s dlsym s n class_func__ _dlerror return dlrunfun fptr NULL NULL Classi Class1 void fptr fptr dlsym g sample dlhandle Classi dClassl chdl if fptr NULL printf Error s dlsym s n class_func__ dlerror return dlrunfun fptr NULL NULL this return int Class1 memfunl int i void fptr int retval fptr dlsym g sample dlhandle Classi memfunl chdl if fptr NULL printf E
26. HANDLE thread N DWORD threadId N int i arg N Create thr new threads for i 0 i lt N i arg i itl thread i CreateThread NULL 0 thread function PVOID arg i 0 amp threadId i if thread i NULL perror Thread creation failed exit EXIT_FAILURE Waiting for threads to finish for i 0 i lt N i if WaitForSingleObject thread i INFINITE WAIT OBJECT 0 perror Thread join failed exit EXIT_FAILURE exit EXIT_SUCCESS Program 1 11 A multi thread program using Windows API executing multiple embedded Ch programs using multiple instances of Ch interpreter threadswin c 21 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 6 REDIRECTING STANDARD OUTPUT AND ERROR STREAM FROM EMBEDDED CH 1 6 Redirecting Standard Output and Error Stream from Embedded Ch Sometimes it is desirable to display error messages in pop up Windows in some GUI applications The simplest method to display a message from a Ch script is to use Windows API MessageBox as shown below This Ch script will display a pop up message in Windows include lt windows h gt define APP NAME MessageBox Title int main PCHAR string This is a message generated from MessageBox n MessageBox NULL string APP NAME MB OK MB SYSTEMMODAL MB NOFOCUS return 0 More demo programs for Ch Windows can
27. abort break case ACTION U chUpStack d break TRUE TEPOVER dgptr E ONTINUE LH dgptr B PSTACK gptr case ACTION DOWNSTACK break case ACTION S chStack dgp break case ACTION L break chVariables break case ACTION A ptr line t chAddWatchE break case ACTION VARIABLE TACK tr OCALS chLocals dgptr dgptr DDWATCH strlen xpr dgp chDownStack dgptr EXP wa 357 line MAX PROMPT STR line line strlen load i line strlen start RUNMODE_START ptr line strlen run R ten 34 ptr case ACTION R EMOVEWA TCH EXPR ptr lin hRemoveWat break Q Fstrlen remov chExpr dgptr ptr case ACTION R EMOVEWA TCH EXPRS chRemoveWat break chExprs dgptr 191 condexpr MAX PROMPT STR N0 ptr Program 7 16 Functions corresponding to the debug user interface menu debug c Contd CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER case ACTION SETBREAKPOINTLINE ptr linetstrlen stopat sscanf ptr s d s d name amp linenum condexpr condtrue chSetBreakPointLine dgptr name linenum condexpr condtrue break case ACTION SETBREAKPOINTFUNC ptr line strlen stopin Sscanf ptr s s d name condexpr condtrue chSetBreakPoi
28. double a 5 1 2 3 4 5 class tag int i int y public tag tag int funcl int j tag tag cout tag tag is called n i 50 tag tag cout tag tag is called n int tag funcl int j cout lt lt tag funcl called n if i int i 20 z i 30 cout lt lt argument i in funcl is true n return 0 int main int i 10 x class tag c class tag c2 2 c funeliiV return 0 Program 7 4 Ch program with with member functions of class cplus cpp 140 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK Ch supports classes in C Program 7 3 can also be used to process a Ch program using classes Pro gram 7 4 defines aclass tag with constructor tag destructor tag and member function func1 and private data i and y The constructor initializes the private variable i with value 50 A variable c and array of class c2 with two elements are instantiated in function main When the member function tag funcl is called inside function main the end block of the if statement will trigger the call back function in Program 7 3 The output from executing the command prog cplus cpp is shown in Figure 7 3 In this case function printCalldata will print out both class and member function names When a member function is called in addition to local variables and function arguments the symbol table
29. lt embedch h gt Ch Reopen Ch Reopen Synopsis include lt embedch h gt ChFile t Ch Reopen ChInterp t interp const char filename const char mode int filedes Purpose Open a new file descriptor and associate an old one with the new one Return Value This function returns a file descriptor if successful and 1 on failure Parameters interp A Ch interpreter filename File name associated with the stream mode The mode of the opened file fildes The file descriptor of STDIN FILENO STDOUT FILENO and STDERR FILENO for the stream to be associated with the filename Description The file descriptors STDIN FILENO STDOUT FILENO and STDERR FILENO correspond to stdin stdout and stderr streams respectively Similar to function freopen the Ch Reopen function first attempts to flush the stream associated with the file descriptor iledes and then close it Failure to flush or close the file successfully is ignored The error and end of file indi cators for the stream are cleared The ChReopen function opens the file whose pathname is the string pointed to by ilename and associates the stream pointed to by the file descriptor with it The mode argument is used just as in function fopen Example Refer to Program See Also Ch Flush Ch Close 290 Chapter A APIs for Embedding Ch lt embedch h gt Ch RunScript Ch RunScript Synopsis include lt embedch h gt int Ch RunScript ChInterp t interp
30. specifies that after Ch executes every count instructions the callback function will be called These in structions are internal to a Ch program They may contain multiple binary operations or even binary C functions To avoid the callback function being called twice at the same executed line a function block will not trigger a block event as shown below for a Ch function int func CH_MASKCALL event int i a l CH MASKBLOCK event m CH MASKEND event return 0 CH MASKRET event Multiple events are specified using the bit wise or operator For example events to trigger the callback function just after a Ch program calls and enters a new function or just before a Ch program leaves a function can be specified by int event CH_MASKCALL CH_MASKRET Ch_AddCallback interp event callback clientdata count For the new line event Ch MASKLINE the non executable lines for the openning block and closing block will not trigger the callback function A line for a declaration statement except for declaration with initialization or declaration of a variable length array will also not trigger the callback function A hidden statement static const char func function name for a function block also will not trigger the callback function when the new line event is specified When the new line event is Ch MASKLINE how the callback function is triggered is shown as follows
31. 128 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION The above event masks can be used to specify events which trigger the callback function To avoid the callback function being called twice at the same executed line the open brace follow ing the closing parenthesis of a function definition only triggers the function call event It will not trigger a block event as shown below for a Ch function int func trigger CH MASKCALL event only int i 2 trigger CH_MASKBLOCK event n trigger CH MASKEND event return 0 trigger CH MASKRET event A function return event can be triggered either by a return statement or the ending of the function block for the return type of void of a function Multiple events are specified using the bit wise or operator For example events to trigger the callback function just after a Ch program calls and enters a new function or just before a Ch program leaves a function can be specified by int event CH MASKCALL CHMASKRET Ch AddCallback interp event callback clientdata count A callback function is disabled by setting the event to Ch MASKNONE The third argument of Ch AddCallback is the callback function A callback function has type ChCallback t defined as follows typedef void ChCallback t ChInterp t interp ChBlock t calldata ChPointer t clientdata The second argume
32. 144 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK File Name prog3 c include lt stdio h gt include lt string h gt include lt embedch h gt void callback ChInterp t interp ChBlock t calldata ChPointer_t clientdata int sysfunc int i printf i in sysfunc is d n i return 0 EXPORTCH int sysfunc chdl void varg ChInterp t interp ChVaList t ap int i retval Ch VaStart interp ap varg i Ch VaArg interp ap int printf i in sysfunc chdl is d n i retval sysfunc i Ch VaEnd interp ap return retval int main hInterp t interp char argvv prog3 ch NULL int mask count 0 ChPointer_t clientdata NULL Q Ch Initialize amp interp NULL Ch DeclareFunc interp int sysfunc int i ChFuncdl t sysfunc chdl mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int p level 0 ChBlock t calldata2 if calldata event amp CH MASKCALL amp amp strcmp calldata funcname sysfunc printCalldata calldata symbols interp while Ch ChangeStack interp level amp calldata2 CH OK printCalldata amp calldata2 symbols interp Program 7 6 C program defines a sy
33. 1iband embedch lib located in the directory CHHOME extern 1ib To add this directory in the library search path as follows Under the same Option Window select Library files under Show directories for add C Ch extern lib under Directories then click OK to finalize the settings in Option Window Finally click Projects gt Settings andselect Link add chsdk 1lib embedch libunder Project Options Also add NODEFAULTLIB LIBCD LIB under Project Options Then click OK to finalize the settings in Project Settings The source code for Visual C 6 0 workspace and project files for examples described in sections and 5 5 can be found in the distribution of Embedded Ch for Windows in the directories HHOME toolkit demos embedch chapters chapter5 embedfuncmain and HHOME toolkit demos embedch chapters chapter5 embedclassmain respectively ou will find VC project files embedfuncmain dsw embedfuncmain dsp mbedclassmain dsw and embedclassmain dsp in these two directories Please check HHOME toolkit demos embedch chapters chapter5 embedfuncmain readme and HHOME toolkit demos embedch chapters chapter5 embedclassmain readme for more details on how to run the program F9 Q 00 1 23 4 Building Executables in Windows Using Intel C Compiler To use Intel C compiler you need to install Visual NET first If Intel C compiler is installed at C Program Files Intel Compiler Ct 10 1 021 1A32 the co
34. 50090 amp sp 50110 amp sp i 50090 The address of the variable 50090 The user defined tag is 59940 udinfo dtype CH STRUCTTYPE 1 The size of the struct class union is 48 he tag name of the struct class union is tag The total number of members is 4 meminfo gt index 0 meminfo gt memname i meminfo gt offset 0 meminfo gt dtype CH INTTYPE 1 meminfo gt ispublic 1 minfo gt index 1 minfo gt memname d minfo gt offset 8 meminfo gt dtype CH DOUBLETYPE 1 meminfo gt ispublic 1 B 3 3 minfo gt index 2 minfo gt memname p minfo gt offset 16 minfo gt dtype CH INTPTRTYPE 1 meminfo gt ispublic 1 BBB B meminfo gt index 3 meminfo gt memname a meminfo gt offset 20 meminfo gt dtype CH INTTYPE 1 meminfo gt ispublic 1 meminfo gt arraytype 80 meminfo gt dim 2 meminfo gt externt 0 meminfo gt externt 1 sll w N I o meminfo offset meminfo offset meminfo offset meminfo offset i 10 d 20 000000 p 500a0 a 0 30 ll 16 20 Figure 2 5 Output from executing userdefinedsymbol c 40 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS KKK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk k k k k k Ck kk kk ck k ck k ck k K I OK File Name expreval c Evaluating expression in Ch space KCKCKCKCKCKCKC
35. CallFuncByName Ch CallFuncByNameVar and Ch CallFuncByA ddr we can call Ch function by expression evaluations with APIs Ch ExprEval and Ch ExprCalc It has been mentioned in the pervious chapter This method can also be used to call Ch functions taking variable length argument lists For example functions func2 and func3 in Program 3 10 are called in Pro gram 3 12 by expression evaluations The result of running executable compiled from Program 3 12 is shown as Figure Note that the function func1 which takes a variable length argument list cannot be called by this method 64 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS include lt stdio h gt include lt embedch h gt int main ChInterp t interp char argvv callchvna ch NULL int retval status int i 5 int j 10 float f 20 double d 40 initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program callchvna ch failed n printf calling func2 from C space Mn int func2 int i Ch ExprEval interp func2 5 10 20 0 Ch ExprEval interp func2 5 10 20 0 40 0 printf Xncalling func3 from C space n int func3 Ch ExprEval interp func3 10 20 0 Ch ExprEval interp func3
36. Calling Ch functions by evaluating expressions KCKCKCKCKCKCKCKCkCKCkCk X kk kk kk kk kk kk kk kk kok kk kok kCkCk ck kckck ck ck ck ck k ck kok ck okokckok ck ok ko ke ke ex kx include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv funcall2 ch NULL void fptr int retval_g initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program funcall ch failed n fptr Ch SymbolAddrByName interp func obtain the address of the function Ch CallFuncByAddr interp fptr amp retval g 10 call Ch function func printf In C after the first function call Mn printf retval g d n retval g Ch Ch ExprEval interp ii ExprEval interp dd dd ii Ch CallFuncByAddr interp fptr amp retval g 20 call Ch function func printf In C after expression evaluations and the second function call n printf retval g d n retval g Ch End interp Program 3 3 Calling Ch function with API Ch CallFuncByAddr funcall2 c The first argument is an instance of Ch interpreter The second argument is the pointer containing the address of the function and the third one is the pointer containing the address of the return value of the called
37. ChInterp t interp ChVaList t ap double x Ch VaStart interp ap varg x Ch VaArg interp ap double funcl x Ch VaEnd interp ap EXPORTCH double func2 chdl void varg ChInterp t interp ChVaList t ap double x y double retval Ch VaStart interp ap varg x Ch VaArg interp ap double y Ch VaArg interp ap double retval func2 x y Ch VaEnd interp ap return retval EXPORTCH struct tag func3 chdl void varg 238 Ch DeclareFunc Chapter A APIs for Embedding Ch lt embedch h gt ChInterp t interp ChVaList t ap struct tag sp S Ch VaStart interp ap varg sp Ch VaArg interp ap struct tag S Ch VaArg interp ap struct tag sp func3 sp s Ch VaEnd interp ap return Sp SPECIAL CASE for function returning a struct EXPORTCH void func4 chdl void varg return type void ChInterp t interp ChVaList t ap struct tag retval return value int i struct tag Ss Ch VaStart interp ap varg retval Ch VaArg interp ap struct tag i Ch VaArg interp ap int s Ch VaArg interp ap struct tag retval func4 i s return value Ch VaEnd interp ap SPECIAL CASE for function with variable number of arguments int EXPORTCH int func5 chdl void varg ChInterp t interp ChVaList t ap void ap c equivalent to va list ap c ChVaList
38. DFREEBSD DQNX should be added when programs such as func c with exported functions are compiled for Windows Linux Solaris HP UX Mac OS X FreeBSD ONX respec tively The binary objects should be linked with library embedch 1ib in Windows or 1ibembedch a in other platforms to form program prog exe In Windows library prog lib will be generated at the same time when prog exe is created This li brary with symbols for exported functions shall be used to create dynamically loaded library 1ibsample dl as shown in Program 5 28 include sample h include lt ch h gt EXPORTCH double funcl double x return 2 sin x EXPORTCH double func2 double x double y return 2 hypot x y Program 5 23 C function file func c 97 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 4 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE ifndef SAMPLE H define SAMPLE H funcl and func2 use sin and hypot in math h include lt math h gt in Ch space ifdef CH extern double funcl double extern double func2 double double include lt dlfcn h gt void _Chsample_handle _Chsample_handle dlopen libsample dl RTLD LAZY if Chsample handle NULL fprintf stderr Error dlopen s n dlerror fprintf stderr cannot get Chsample handle in sample h n exit 1 void _dlclose_sample void dlclose Chsample handle
39. Microsoft MS DOS Windows Windows 95 Windows 98 Windows Me Windows NT Windows 2000 and Windows XP are trademarks of Microsoft Corporation Solaris and Sun are trademarks of Sun Microsystems Inc Unix is a trademark of the Open Group HP UX is a trademark of Hewlett Packard Co Linux is a trademark of Linus Torvalds QNX is a trademark of QNX Software Systems All other trademarks belong to their respective holders il Preface Ch is an embeddable C C interpreter Ch supports all features in the ISO 1990 C standard most new features in the latest ISO C99 standard including complex numbers and variable length arrays classes in POSIX C Win32 X Motif OpenGL GTK ODBC WinSock very high level shell programming cross platform internet computing in safe Ch computational arrays for linear algebra and matrix computa tions high level 2D 3D plotting and numerical computing such as differential eguation solving integration Fourier analysis Ch can also be used as a login Unix command shell and for high level scripting such as shell programming to automate tasks and common gateway interface in a Web server in both Unix and Windows Embedded Ch includes distributable Embedded Ch Standard or Professional Edition It reguires a regu lar Ch Standard or Professional Edition and Ch SDK installed first In most platforms Ch SDK is bundled with both Ch Standard and Professional Editions By default Embedded Ch includes distributable Embedde
40. The variable meminfo of structure ChMemInfo t contains all necessary information for a member of user defined type Whether if it is constructor destructor or member func tion can be determined by the values of meminfo isconstructor meminfo isdestructor or meminfo ismemberfunc The value meminfo dtype gives the data type of the member The address for the member is obtained by the expression memaddr char addr meminfo offset If the member itself is user defined type the value meminfo udtag contains the tag for the user defined type The nestlevel is incremented for printing the names and values of its members For a member of array type it is handled similar to a regular array as in function chPrintSymbolValue The value meminfo dim gives the dimension of the array The array meminfo extent contains the extents for each dimension of the array up to 7 dimension Similar to Program 7 3 three sample scripts will be processed by Program 7 8 Functions func1 and main in Program 10 are the same as ones in Program Program 7 10 contains a header file stdio h and more global variables of different data types The output from executing the command prog4 prog4 ch 155 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES include lt stdio h gt include lt stdbool h gt int i 40 ptri double g 100 double a 5 1 2 3 4 5
41. c are mainly for callback interface with Embedded Ch Functions in menu c are for user interface File debug c contains functions for initializing and cleaning up a debugger functions corresponding to commands in the user interface shown in Figure 7 10 and function for processing the interactive user input menu The sample debugger in Program 7 14 can debug multiple programs one at a time in each iteration in a while loop Program 7 14 first calls function chHe 1p in Program 7 15 to display the debug menu shown in Figure 7 10 It then enters an infinite while loop for menu driven debugging A debugger is initialized by the function chInitDebug defined in Program 7 16 The output and error streams are set without buffer by the statements setbuf stdout NULL no buffer for debugging purpose setbuf stderr NULL no buffer for debugging purpose so that the proper sequence for input and output can be maintained when this sample code is modified for a GUI based debugger In Windows the input and output might need to be redirected by function Ch Reopen Each element for command line options in dgpt r argvv is set to NULL For an appli cation with its customized distribution of Embedded Ch the member dgpt r opt ion needs to be setup properly as shown in Program 7 17 For statements in function ChInitDebug dgptr gt callback chCallback dgptr clientdata ChPointer t dgptr the member callba
42. c_i assign amp c_data in C space to ch_data in Ch space Ch_SetVar interp ch_data CH_INTPTRTYPE amp c_data Ch ExprEval interp printf ch_data 9 d n ch_data 9 Ch End interp Program 2 5 Assigning values in C space to variables in Ch space setvar c Ch SetVar interp ch i CH INTTYPE c i Ch SetVar interp ch p CH INTPTRTYPE sc 1 The value for c i in the C space is later changed to 30 indirectly through the indirection operator in the Ch space by function call Ch ExprEval interp ch p 30 The array c data in the C space is shared in the Ch space by the variable ch data The output from executing Program 2 5 is shown in Figure 2 3 Variables in the Ch space in Program 5 are created dynamically by function Ch AppendRunScriptO Variables in a Ch script can also access values in the C space as shown in Program Program is similar to Program 2 5 Program 2 6linvokes embedded Ch program set var ch in Program 2 7 Variable c i and its address are assigned to variables ch i and ch p in Program 2 7 Array c data in the C space is shared by variable ch data in Program 2 7 Function chfun in the Ch space is called by Ch CallFuncByName interp chfun NULL in the C space which changes the values for elements c data 0 and c data 9 in the C space Details about calling a Ch function using the API Ch CallFuncByName will be described in detail Chapter 3 The output from
43. code double func int n int retval retval 2 n return retval int main printf hello n return 0 int c i 10 double retval Ch Initialize amp interp NULL call Ch function by its name Some API hypot amp retval 3 0 4 0 printf retval f n retval Ch AppendRunScript interp code Some API func amp retval c i printf retval f n retval Ch End interp return 0 Program 3 6 Calling Ch functions using user s own API through function Ch CallFuncByNamev 54 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 4 CALLING CH FUNCTIONS AND ACCESSING CH VARIABLES CREATED IN C File funcall4 c include lt stdio h gt include lt embedch h gt int main ChInterp t interp char code double func int n int retval retval 2 n return retval int main printf hello n return 0 int c i 10 double retval h Initialize amp interp NULL h CallFuncByName interp hypot amp retval 3 0 4 0 rintf retval f n retval h AppendRunScript interp code h CallFuncByName interp main amp retval h CallFuncByName interp func amp retval c i rintf retval f n retval h_AppendRunScript interp int ch i h SetVar interp ch i CH INTTYPE c i h ExprEval interp ch i 5 h_ExprCalc interp 10 ch_ithypot 3 4 CH_DOUBLETYPE amp retval printf retval f n retval Ch End interp return
44. data i return 100 this function is called from the Ch space EXPORTCH int printdata chdl void varg ChInterp t interp ChVaList t ap int num retval double data Ch VaStart interp ap varg num Ch VaArg interp ap int data Ch VaArg interp ap double printf data in printdata in C is retval printdata num data Ch VaEnd interp ap return retval Sp n data Program 5 20 A C program calling back a C function from a Ch function without a function file prog3 c 94 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 4 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE File embedch3 ch include lt stdio h gt include sample h the function main is optional int main double data 4 1 0 2 0 3 0 4 0 printdata 4 data return 0 Program 5 21 Ch program embedch3 ch 5 4 Calling C Functions in the Main Program from Ch Space with dl File In the example in section file sample_chdl c with interface functions shown in Program is compiled and linked with the application program without a separate dynamically loaded library dl file An alternative method with a dynamically loaded library dl file has been used in the past Although this method has been deprecated it is described in this section using the same example presented in section Functions funcl and func2 called from Ch space are located inside the executable binary
45. dlclose ChExpvar handle atexit dlclose expvar fendif Get addresses of global variables in C space void fptr expvar h fptr expvar h dlsym ChExpvar handle expvar chdl if fptr expvar h NULL printf Error s dlsym s n _ func dlerror return dlrunfun fptr expvar h NULL NULL amp il 4i2 fendif Program 4 8 Ch header file for handling global variables expvar h 72 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 3 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE WITH DL FILE include expvar h void changeCVar void fptr fptr dlsym ChExpvar handle changeCVar_chdl if fptr NULL printf Error s dlsym s n func p dlerror return Pass the address of retval to C space dlrunfun fptr NULL NULL return int main printf Original values of il and i2 in Ch are d and d n il i2 changeCVar change variables in C space printf After calling changeCVar il and i2 are d and d n il i2 return 0 Result from excuting this program The variables in the C space are 100 and 200 Original values of il and i2 in Ch are 100 and 200 The variables in the C space are 100 and 400 After calling changeCVar il and i2 are 100 and 400 Program 4 9 expvar ch Ch application accessing global variables in C space 4 3 Accessing Variables
46. double dd 1 1 int p amp ii int a 3 1 2 3 struct tag int i double f J s 10 20 printf printed from expreval ch ii d dd Sf n ii dd int func printf func in expreval ch called ii d dd f n ii dd return 0 Program 2 11 Evaluating expressions in a Ch program expreval ch printed from expreval ch ii 10 dd 1 100000 In C befor xpression evaluations ii 10 dd 1 100000 p 10 a 0 1 a 1 2 a 2 3 s i 10 s f 20 000000 func in expreval ch called ii 11 dd 30 100000 In C after expression evaluations ii 11 dd 30 100000 p 11 Figure 2 6 Output from executing expreval c 42 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS in the Ch space based on the current values of two variables ii and dd The output from executing the file built from expreval c is shown in Figure N C Space expreval exe C lt Ch Space i main expreval ch Ch Initialize i iit lt i Ch RunScript l n Ch ExprEval interp ii Ch_ExprEval interp dd dd ii l ne i C Je Ch End J Figure 2 7 Evaluating expressions in Ch program Before an expression in Ch space is evaluated by function Ch ExprEval it can be parsed to test if it is syntactically correct For example the expression evaluation statement Ch E
47. extern in extern in extern in extern in extern in extern in extern in extern in extern in extern in extern in extern in CE GD cb ot Ch Ch CR ugh etek Gr oc sce ct C o C C C C o C o c c Cc C o extern in Program 7 13 The header file with defined data structures and function prototypes for the debug program debug h Contd 176 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM extern extern extern extern extern extern extern extern extern extern extern extern extern extern extern extern fun extern ifdef fendif fendif in in in in in int in in in in in in in in in in cti CB Seber et ct CL OCR ZC CE CES eb Gk Gh CT C C C C Cc o C o o c 7 4 A SAMPLE DEBUGGER hAddWatchExpr debugInfo t dgptr char exprname hProcessWatchExprs debugInfo t dgptr hRemoveWatchExpr debugInfo t dgptr char exprname hRemoveWatchExprs debugInfo t dgptr hSetBreakPointLine debugInfo t dgptr char filename int linenum char condexpr int condtrue hSetBreakPointFunc debugInfo t dgptr char funcname char condexpr int condtrue hSetBreakPointVar debugInfo t dgptr char varname char condexpr int condtrue hClearBreakPointLine debugInfo t dgptr char filename int linenum hClearBreakPointFunc debugInfo t dgptr char funcname hClearBreakPointVar debugInfo t dgptr char
48. fprintf stderr cannot get g sample dlhandle in sampclass h Nn exit 1 void _dlclose_sampclass void dlclose g sample dlhandle atexit dlclose sampclass fendif fendif class Classl ifndef CH for C space KK RR ke e x x x x private int mil fendif public Class1 Class1 int memfunl int i hi pragma importf lt sampclass chf gt endif SAMPCLASS H Program 5 29 C Ch header file sampclass h 101 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE include lt stdio h gt finclude sampclass h Class1 Class1 mil 1 printf m il in Classl Classl d n m i1 Classl Classl printf m_il in Classl Class1 d n m i1 int Class1 memfunl int i m il i printf m_il in Classl memfunl d n m i1 return m_il Program 5 30 C class member functions file sampclass cpp 102 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE include lt stdio h gt finclude ch h finclude sampclass h member functions of Classl EXPORTCH void Classl Classl chdl void varg ChInterp t interp ChVaList t ap class Classl c new Class1 Ch VaStart interp ap varg Ch CppChangeThisPointer interp c sizeof Class1 Ch VaEnd interp ap EXPORTCH void Class
49. g sample dlhandle dlopen libsampleclass dl RTLD LAZY which loads the module explicitly in Program 5 43 The statement extern void Chsample handle dlopen libsampleclass dl RTLD LAZY loads the dynamically loaded library when the code is parsed To use an instance of a class in a global variable one may declare the handle as a global variable in the Ch space as follows void Chsample handle When a program is executed by API Ch RunScript or Ch ExecScript the destructor of the class will delete the instance of a class in the global variable in the program In this case the library can be explicitly loaded in the Ch space as follows Ch Initialize interp option Ch ParseScript interp prog Ch ExprEval interp Chsample handle dlopen libsampleclass dl RTLD LAZY Ch ChCallFuncBYName interp Cplusfunc retval arg use an instance of class Makefiles in Program 5 44 and Program 5 45 can be used to compile Program 5 31 for Unix and Win dows respectively to create a dynamically loaded library 1ibsampclass dl In Windows the exe cutable binary program prog exe should be created first The library prog 1ib will be generated at the same time when prog exe is created This library with symbols for exported member functions shall be used to create dynamically loaded library 1ibsampclass dl as shown in Program 5 45 Note that for command dllink the option embedded is required to make dynamically loa
50. in the Ch space Ch DeclareFunc interp double funcl double x funcl chdl Ch DeclareFunc interp double func2 double x double y func2 chdl run a Ch program status Ch RunScript interp argvv if status printf Error execution of program ch failed n Ch End interp Program 5 12 C program invoking embedded Ch program calling C function using Ch DeclareFunc prog c File name sample chdl h ifndef SAMPLE CHDL H define SAMPLE CHDL H include ch h EXPORTCH double funcl chdl void varg EXPORTCH double func2 chdl void varg endif SAMPLE CHDL H Program 5 13 C header file sample chdl h 87 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File name sample h ifndef SAMPLE H define SAMPLE H funcl and func2 use sin and hypot in math h include lt math h gt if defined CH in C space extern double funcl double x extern double func2 double x double y fendif endif SAMPLE H Program 5 14 C Ch header file sample h 5 3 Calling Back C Functions in the Main Program from Ch Space In this section callback C function in the main program from a function in the Ch space will be demonstrated using examples In Program 5 15 the script in Program 5 T6 is executed first The script contains a function printdata
51. in the Ch space Without the main function or function printdata is not called explicitly this function will not be executed when the script is invoked by function Ch RunScript in Program 5 15 The function printdata inthe Ch space is called explicitly by the function Ch CallFuncByName in the C space The function returns an integer of type int The values for two arguments of the function with num of type int and data of array of double are passed from the function Ch_CallFuncByName The function printdata in the Ch space calls the same function in the C space inside Program 5 15 through a wrapper function printdata chdl The header file sample h in Program 5 T7 obtains the handle for dynamically loaded module needed inside the function printdata in the Ch space If there are multiple functions the definitions for functions such as printdata in the Ch space can be placed in function files The output from execution of Program 5 15 is displayed in Figure 5 2 In Programs 5 15 and 5 16 the callback function from Ch space is fixed In some applications it might be desirable to call different callback functions in the C space from a single Ch function This next example illustrates the technique for handling such a callback Program 5 18 loads the script in Program 5 19 first When the function callback is invoked explicitly by the function Ch CallFuncByName in the C space The actual callback function printlist
52. kk include lt embedch h gt include lt stdio h gt include lt string h gt int main ChInterp t interp int status char argvv embedchl ch NULL initialize embedded Ch Ch Initialize amp interp NULL parse embedded Ch program indicated by argvv status Ch ParseScript interp argvv if status CH ERROR printf Error parse program embedchl ch failed n else execut mbedded Ch program embedchl ch status Ch ExecScript interp argvv 0 if status printf Error execute script embedchl ch failed n release memory and terminate the shell Ch End interp Program 1 4 A C program with embedded Ch with separate parse and execution embedchl b c 10 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 3 OTHER ESSENTIAL APIS AND OPTIONS FOR EMBEDDED CH default options of Ch shell are used In Program we will describe how to use user defined options with the data type ChOptions t The variable opt ion is declared as a structure of type ChOptions t with member fields shelltype and chhome as follows typedef struct ChOptions int shelltype shell type char chhome Embedded Ch home directory ChOptions t e Field shelltype indicates the type of the Ch shell It could be CH REGULARCH for a regular Ch shell or CH SAFECH for a safe Ch shell By default its value is CH REGULARCH e Field chhome contains the home directory for the Embe
53. lt stdio h gt include lt string h gt include lt stdlib h gt include lt embedch h gt void callback ChInterp t interp ChBlock t calldata ChPointer_t clientdata int printCalldata ChBlock t calldata extern void chPrintSymbolValuesInStack ChInterp t interp int level int main int argc char argv ChInterp t interp char argvv int mask count 0 ChPointer t clientdata NULL if argc 2 printf Usage s chscriptname n argv 0 exit EXIT_FAILURE create char argvv chscriptname NULL dynamically argvv char malloc 2 sizeof char argvv 0 argv 1 argvv 1 NULL Ch Initialize amp interp NULL mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int level 0 ChBlock t calldata2 if calldata gt event amp CH MASKEND amp amp strcmp calldata funcname funcl printCalldata calldata chPrintSymbolValuesInStack interp calldata gt level while Ch ChangeStack interp level amp calldata2 CH OK printCalldata amp calldata2 chPrintSymbolValuesInStack interp calldata2 level Program 7 8 C program for printing values of all variables within its scope prog4 c 148 CHAPTER 7 THE DEBUG AND
54. name s address index name addr p int Ch GlobalSymbolAddrByName interp if p NULL printf i is not available n else printf i Sdin p if Ch VarType interp localvariable ChBlock t calldata CH GLOBALVARTYPI ChPointer t clientdata clevel hlevel totalnum index index p Nn i 1 printf localvariable is not CH_GLOBALVARTYPE n if Ch VarType interp printf func is CH_GLOBALVARTYPE n ifunc CH GLOBALVARTYPE globalsymbol ch A Ch program with global variables and functions int o0 278 Chapter A APIs for Embedding Ch lt embedch h gt double d 1 1 int prototype extern int e struct tag int jz s int func int localvariable 20 printf printed from globalsymbol ch i d return 0 int main func return 0 Output printed from globalsymbol ch i 10 d 1 100000 the current stack level is 0 the highest stack level is 2 The total number of symbols is 7 symbol index 0 name i address 5a000 symbol index 1 name d address 58380 symbol index 2 name prototype address 0 symbol index 3 name e address 0 symbol index 4 name s address 54028 symbol index 5 name func address 45e90 symbol index 6 name main address 51938 i 10 localvariable is not CH GLOBALVARTYPE func is CH GLOBALVARTYPE Nu BUN See Also Ch GlobalSymbolTotalNum Ch_Glob
55. of f6 is tag of f6 is tag of f6 is tag 0 00 Dona o VW O 0 90 090 0 0 0 0 0 0 0 0 0 0 0 0 000000 KO 0 UO G A UN I2 4S o Ja G A UN EA o See Also Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgArrayType Ch FuncArglIsFunc Ch FuncArgIsFuncVarArg Ch FuncArgFuncArgsNum Ch FuncArgUserDefinedName Ch FuncArgUserDefinedSize 268 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgFuncArgNum Ch FuncArgFuncArgNum Synopsis include lt ch h gt int Ch_FuncArgFuncArgNum ChInterp t interp const char funcname int argnum Purpose Obtain the number of the arguments of the function type for an argument of a function in the Ch space Return Value This function returns the number of the arguments of the function type for an argument of a function in the Ch space If the argument is not function type it returns 1 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description This function gets the number of the arguments of a function or pointer to function type for an argument of a function in the Ch space If the function takes a variable number of arguments it only returns the fixed number of arguments In this case function Ch FuncArgIsFuncVarArg returns 1 Example See Ch_FuncArgDataType See Also Ch_FuncArgDataType Ch FuncArgArrayD
56. status pthread create sthreadId i NULL thread function argti if status 0 perror Thread creation failed exit EXIT_FAILURE Waiting for threads to finish for i 0 i lt N itt status pthread_join threadId i NULL if status 0 perror Thread join failed exit EXIT_FAILURE exit EXIT_SUCCESS Program 1 10 A multi thread program using POSIX pthread executing multiple embedded Ch programs using multiple instances of Ch interpreter threads c 20 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS KK kk kk kk kk kk kk kk KK k K k kk KK KKK KA KCKCKCKCKCKCkCKCkCk kk k k k k k k KCk k k ck k k ck ck k ck ko kk File Name threadswin c Embeddeding Ch in a multi thread application in Windows OKCKCKCKCkCKCKCkCKCkCKCkCKCkCKCKCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCkCKCKCKCkCk k k k k kc k ck k ck k k k k k k kk f include lt windows h gt include lt stdio h gt include lt stdlib h gt include lt embedch h gt define N 3 char code int func int i printf d hello n i return 0 DWORD WINAPI thread_function PVOID arg ChInterp t interp int val retval val int arg Ch Initialize amp interp NULL Ch AppendRunScript interp code Ch CallFuncByName interp func amp retval val Ch End interp return 0 int main
57. tion funcl Lion funel current fil number is 14 starts at line 5 ends at line 15 function name is funcl The symbol symbol symbol symbol symbol symbol i 30 0 index index index index index index Or WN F The stack level The current lin name is progl ch total number of symbols is 6 address 59ad0 z address 59b00 func address 57968 g address 5b218 y address 5b248 i address 4c000 name i name name name name name is 1 number is 20 Function main Function main starts at line 17 ends at line 22 The function name is main Th The symb symb symb i The Th current file name is progl ch total number of symbols is 3 ol index 0 name _ func address 4f900 ol index 1 address 531b0 ol index 2 address 4bf50 10 name i name x Stack level is 2 current line number is 0 Th The symb symb symb symb symb i current file name is progl ch total number of symbols is 5 ol index 0 name i address 44f90 ol index 1 name g address 53750 ol index 2 name a address 506f8 ol index 3 name funcl address 50760 ol index 4 name main address 52140 40 Figure 7 2 Output from executing prog2 progl ch 139 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK File cplus cpp include lt iostream h gt using namespace std int i 40 double g 100
58. version 3 5 SoftIntegration Inc 2003 This document describes Common Gateway Interface in CGI classes with detailed references for each member function of the classes Table of Contents ii 1 erm 1 1 4 Jsing Command Shell 3 6 4 4 Go Es 4 i 7 8 8 2 8 mm s 9 Oe 9 14 In remental Execution b Ap pending ode to an Embedded Ch Program 13 POP 15 e edde 22 25 28 29 29 33 35 37 44 A lt Space 47 3 1 Calling Ch Functions by Expression EvaluationS lt lt lt lt lt lt lt lt 2 aaa 47 2 alling b Functions b Address Usine Ch CallFuncByAddrO and Ch_CallFuncByAddrv Q 49 i 51 55 56 60 66 66 68 73 vi 78 78 86 88 95 99 110 Calling C C Functions from ch Space 116 Shar tic se dees ay ar NE 116 h Functions with Arguments of VLAS 20 20 2 2 20 22 116 Calling Ch Functions with Arguments of Assumed Shape Arrays 117 e e 118 121 125 alline 6 1 1 1 128 128 135 Manipulating Local and Global Variablel lt lt lt lt lt lt lt lt s s cen 147 7 3 1 Obtaining Values of Vartablesl lt lt lt oaoa a 147 32 hanging Values of Variable 22s 161 iur aoa ae e a ouod xig eo mx e doa dede HG a USA dod des 166 PS IE 173 V A Sample Debugger Using vu Ch ExecScHDEIM uot ale bd mde
59. with exported functions are compiled for Windows Linux Solaris HP UX Mac OS X FreeBSD QNX respectively The binary objects should be linked with libraries chsdk lib and embedch lib in Windows as shown in Program 5 T or libchsdk a and libembedch a in other platforms to form program prog exe target prog exe progl exe prog exe prog o func o sample chdl o ch dllink prog exe embedded prog o func o sample chdl o prog o prog c ch dlcomp libsample dl prog c progl exe progl o func o sample chdl o ch dllink progl exe embedded progl o func o sample chdl o progl o progl c ch dlcomp libsample dl progl c func o func c ch dlcomp libsample dl func c sample chdl o sample chdl c ch dlcomp libsample dl sample chdl c I clean rm f o exe Program 5 9 Makefile to create prog exe for Unix Makefile target prog exe progl exe prog exe prog obj func obj sample chdl obj ch dllink prog exe embedded prog obj func obj sample chdl obj prog obj prog c ch dlcomp libsample dl prog c progl exe progl obj func obj sample chdl obj ch dllink progl exe embedded progl obj func obj sample chdl obj progl obj progl c ch dlcomp libsample dl progl c func obj func c ch dlcomp libsample dl func c sample chdl obj sample chdl c ch dlcomp libsample dl sample chdl c I clean del obj del exp del lib del exe Program 5 10 Makefile to create prog exe for Windows Makefile 85 CHAPTER 5 CAL
60. 0 Ga x Oy LO GA OGWY 0 O0 Program 3 7 Calling Ch function and accessing Ch variable created in the C space 3 4 Calling Ch Functions and Accessing Ch Variables Created in C In some applications the user provides mathematical expressions and formulas in C syntax These mathe matical expressions and formulas can be sent to an embedded Ch for processing directly Functions in func tion files can also be used directly in embedded Ch Variables and functions in the Ch space can be added by APIs Ch AppendParseScript and Ch AppendRunScript without calling Ch RunScript first then used by APIs Ch ExprParse Ch ExprEvalO Ch ExprCalc Ch CallFuncByAddr Ch CallFuncByName and Ch_CallFuncByNameVar For example Program 3 7 calls function hypot in a function file directly by Ch CallFuncByName interp hypot amp retval 3 0 4 0 The third argument passes back the returned value in the called function Functions func and main are first added to the Ch space by Ch AppendRunScript interp code then called by Ch_CallFuncByName Function main is optional Unlike using Ch RunScript function main loaded by Ch AppendRunScript or Ch AppendRunScriptFile has to be called explicitly using Ch_CallFuncByName or other APIs Next function Ch AppendRunScript is used to declare variable ch i in the Ch space The variable ch i of int type in the Ch space is assigned wit
61. 0 Program 7 1 A Ch program progl ch sp struct tag clientdata o printf sp gt i d sp gt d Sf n sp gt i sp gt d The member calldata funcname of the calldata contains the name of the active function when the callback was triggered When the callback function is called it first prints out the information for its active function by calling function printCalldata The function printCalldata first prints out the event which trig gered the callback function It then prints out other relevant members of the calldata including the count for the event Ch MASKCOUNT the stack level which will be described in the next section the current line number where the program is executing The current line number is useful for many applications For ex ample an IDE may highlight the current executed line a debugger may trace the number of the times when a specified line is executed If the executed code is within a function it will print the line numbers for the beginning and end of the active function and function name If the active function is a member function of a class it wil print the the class name Whether the active member function is a constructor or destructor can be determined by the members isconstructor and isdestructor respectively Member source of ChBlock t contains the file name if the source of the executed code is a file Otherwise it is a string such as when the code is loaded from the memor
62. 0 printf arg 2 of f8 is function d n Ch FuncArgIsFunc interp f8 1 printf arg 3 of f8 is function d n Ch FuncArgIsFunc interp f8 2 printf arg 1 of fl is variable arg din Ch FuncArgIsFuncVarArg interp f1 0 printf arg 4 of fl is variable arg dMn Ch FuncArgIsFuncVarArg interp f1 3 printf arg 1 of f7 is variable arg d n Ch FuncArgIsFuncVarArg interp f7 0 printf arg 2 of f7 is variable arg din Ch FuncArgIsFuncVarArg interp f7 1 printf arg 3 of f7 is variable arg d n Ch FuncArgIsFuncVarArg interp f7 2 printf arg 4 of f7 is variable arg din Ch FuncArgIsFuncVarArg interp f7 3 printf arg 1 of f8 is variable arg dMn Ch FuncArgIsFuncVarArg interp f8 0 printf arg 2 of f8 is variable arg din Ch FuncArgIsFuncVarArg interp f8 1 printf arg 3 of f8 is variable arg din Ch FuncArgIsFuncVarArg interp f8 2 printf func arg num for arg 1 of fl is d n Ch FuncArgFuncArgNum interp f1 0 printf func arg num for arg 4 of fl is d n Ch FuncArgFuncArgNum interp f1 3 printf func arg num for arg 1 of f7 is dWMn Ch FuncArgFuncArgNum interp f7 0 printf func arg num for arg 2 of f7 is dWMn Ch FuncArgFuncArgNum interp f7 1 printf func arg num for arg 3 of f7 is dWMn Ch FuncArgFuncArgNum interp f7 2 264 Chapter A APIs for Embedding Ch lt embedch h gt interp interp interp interp
63. 1 if Ch_ChangeStack dgptr gt interp dgptr gt level amp calldata CH OK if for a GUI based debugger update the edited source code in a GUI window using info in members of calldata and dgptr gt calldata here printf The current line number is d n dgptr gt calldata gt linecurrent printf The line number in the calling function d n calldata linecurrent return 0 else return 1 int chDownStack debugInfo t dgptr ChBlock_t calldata if dgptr gt progname NULL printErrorNoFile return 1 if Ch_ChangeStack dgptr gt interp dgptr level amp calldata CH OK return 0 else return 1 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 182 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM int int int chStack debugInfo t dgptr ChInterp t interp int level isfunc char name classname if dgptr progname NULL printErrorNoFile return 1 interp dgptr gt interp level dgptr gt level printf Stack Level while name Ch_StackNam 7 4 A SAMPLE DEBUGGER Stack Name n level amp isfunc amp classname interp if isfunc 1 printf 4d else if isfunc 2 printf 4d se printf 4d level oo oo el oe s n return 0 chLocals debugInfo_t dgptr ChInterp_t interp int lev
64. 1 ACCESSING C C VARIABLES FROM CH SPACE THROUGH FUNCTION ARGUMENTS include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv embedch4 ch NULL double c_d 10 int c_data 10 1 2 3 4 5 6 7 8 9 10 initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program embedch4 ch failed n printf in embedch4 c c d f c data 0 d c data 9 d n c d c data 0 c data 9 Ch CallFuncByName interp chfun NULL sc d c data printf in embedch4 c c d f c data 0 d c data 9 d n c d c data 0 c data 9 c d 100 change the variable value in C space c data 0 101 change the variable value in C space c data 9 110 change the variable value in C space Ch CallFuncByName interp chfun NULL sc d c data printf in embedch4 c c d f c data 0 d c data 9 d n c d c data 0 c data 9 Ch End interp return 0 Program 4 1 Example of sharing data in C and Ch spaces embedch4 c 67 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 2 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE include lt stdio h gt include lt math h gt the main function is not needed in this example int main printf em
65. 100 after calling the printdata in Ch Figure 5 2 Output of executable compiled from Program scribed in the section 5 2 No function file is needed to call a binary function using its corresponding system function in the Ch space Then Programs and can be modified as shown in Programs and respectively Program 5 20 defines system function printdata using the function call below Ch DeclareFunc interp int printdata int num double data printdata chdl Similarly Program 5 T8 can be replaced by Program 5 22 which contains a function call Ch DeclareFunc interp void callback void func char list callback chdl 91 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File prog2 c include lt stdio h gt include lt string h gt include lt embedch h gt char list stringl string2 NULL void printlist char list typedef void FUNCPTR char int main int argc char argv ChInterp t interp ChOptions t option int status char argvv embedch2 ch NULL Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error Ch ParseScript failed n exit 1 printf calling the callback n status Ch CallFuncByName interp callback NULL printlist list printf after calling the callback n if status CH
66. 3 printf func2 3 call C binary function funcl 3 call C binary function func2 Mn dl r j SE n d2 Program 5 2 Ch Program program ch func1 3 0 in prog c 0 282240 sin 3 0 141120 hypot 3 4 5 000000 func1 3 0 282240 func2 3 4 10 000000 Figure 5 1 Output of executable compiled from Program 5 1 79 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE include sample h double funcl double x return 2 sin x double func2 double x double y return 2 hypot x y Program 5 3 C function file func c shown in Program 5 3 as well as two mathematical functions sin and hypot declared in the stan dard C library header file math h Program 5 2 is the same as one described in detail in Chapter 3 of Ch SDK User s Guide In that example C functions func1 and func2 are located in a binary library Therefore a separate dynamically loaded library 1ibsample dl needs to be created to interface these binary C functions In a typical application with Embedded Ch C functions such as unc1 and func2 which might be in a static or dynamical library are located inside the executable binary program that invokes embedded Ch programs To call these C functions from a Ch script these functions shall be exported so that Ch programs can callback these binary functions We will demonstrates the mecha
67. 6 is struct Mn if Ch FuncArgDataType interp f7 0 CH INTTYPE printf arg 1 of f7 is int Mn if Ch FuncArgDataType interp f7 1 CH INTTYPE printf arg 2 of f7 is int Wn if Ch FuncArgDataType interp f7 2 CH INTTYPE printf arg 3 of f7 is int n if Ch FuncArgDataType interp f8 0 CH INTTYPE printf arg 1 of f8 is int n if Ch FuncArgDataType interp f8 1 CH INTTYPE printf arg 2 of f8 is int Wn 262 Chapter A APls for Embedding Ch lt embedch h gt Ch FuncArgDataType if Ch FuncArgDataType interp f8 2 CH INTTYPE printf arg 3 of f8 is int n if Ch_FuncArgArrayType interp f1 0 printf arg 1 of f1 is not array n if Ch_FuncArgArrayType interp f1 4 printf arg 4 of fl is not array n if Ch_FuncArgArrayType interp f2 0 printf arg 1 of f2 is not array n if Ch_FuncArgArrayType interp f4 1 CH CARRAYPTRTYPE printf arg 2 of 4 is pointer to C array n if Ch_FuncArgArrayType interp f4 2 CH CARRAYVLATYPE printf arg 3 of 4 is C VLA array n if Ch_FuncArgArrayType interp f4 3 CH CARRAYVLATYPE printf arg 4 of f4 is C VLA array n if Ch_FuncArgArrayType interp f4 4 CH CARRAYVLATYPE printf arg 5 of 4 is C VLA array n if Ch FuncArgArrayType interp f5 1 CH CHAR
68. ACTION_REMOVEWATCHEXPRS command remove watch expressions one may click a line in an edited code in GUI to set a new breakpoint ACTION SETBREAKPOINTLINE command stopat filename cond in file at line ACTION_SE REAKPOINTFUNC command stopin funcname cond in function ACTION_SETBREAKPOINTVAR command stopvar varname cond when the value of a variable changes one may use a popup dialog to clear a breakpoint ACTION_CLEARBREAKPOINTLINE command clearline filename ACTION_CLEARBREAKPOINTFUNC command clearfunc funcname ACTION_CLEARBREAKPOINTVAR command clearvar varname ACTION_CLEARBREAKPOINTS command clear ACTION_HELP command help w Use as commands in an interactive I O window to assign a value call a functoin and print an expression ACTION_ASSIGN command assign var expr ACTION_CALL command call func ACTION PRINT command print expr ACTION EXPR command expr ACTION ABORT command abort action t define MAX NUM ARGS 100 maximum number of arguments for commands start and run Eo Program 7 13 The header file with defined data structures and function prototypes for the debug program debug h k 174 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE
69. Afterwards it calls function symbols to process the variables within its scope inside the function In function symbols first the total num ber of symbols in the symbol table for the active function is obtained by the API Ch_SymbolTotalNum Next a for loop is used to walk through each variable in the symbol table by its index The first vari able in the symbol table has index 0 The name and address for each variable are obtained by functions Ch_SymbolNameByIndex and Ch_SymbolAddrByIndex respectively and are printed out Finally if the identifier i is a valid variable when the callback is called its value will be printed out After functions printCalldata and symbols are called once function callback enters a while loop to process calling functions recursive by changing the function stack As an example when Program 7 1 is processed by Program 7 3 using the following command prog progl ch the output is shown in Figure 7 2 When the callback is triggered by the end block event of an if statement in Program 7 1 it will print out the information for the active function func1 first Then all variables in its symbol table starting with one closed to the executed statement are displayed In this case the symbol consists of local variables i and z inside the block local variables _func_ g y and argument i of the function The local variable func of char array contains the called function name which is a feature
70. Ch SymbolAddrByName Ch SymbolNameByIndex Ch Symbollndex Ch SymbolIndexByName Ch SymbolTotalNum Ch UserDefinedInfo Ch UserDefinedMemInfoByName Ch UserDefinedMemInfoByIndex Ch UserDefinedName Ch UserDefinedSize Ch UserDefinedTag Ch VarType Macros Obtain the data type of an argument of a function Obtain the number of arguments of a function Determine if an argument of function is function type Determine if an argument of function is a function with a variable number of argument Obtain the number of the arguments of a function Obtain the name of the user defined data type of an argument of function Obtain the size of the user defined data of an argument of function Determine the function type of a variable Get the global user data set by Ch SetGlobalUserData Get the address of a global variable based on its index in the symbol table Get the address of a global variable based on its name Get the name of a global variable based on its index in the symbol table Obtain the index number of a global variable based on its name Obtain the total number of symbols of global variables in the symbol table enable disable initialization of global variables at parse time Initialize embedded Ch for executing a Chprogram Obsolete use Ch FuncType Determine if the variable is function with a variable number of arguments Obsolete use Ch VarType Parse a Ch program in C space Open a new file descriptor and assoc
71. Ch can be changed in a dynamically loaded object Example See an example in Ch_DataType See Also Ch GlobalSymbolAddrByName Ch CallFuncByAddr Ch_CallFuncByName 305 Chapter A APIs for Embedding Ch lt embedch h gt Ch SymbolIndexByName Ch SymbolIndexByName Synopsis include lt ch h gt int Ch_SymbolIndexByName ChInterp_t interp const char name Purpose Get the index number of a variable in the symbol table for variables and arguments of a function or in the symbol table for global variables in a Ch program Return Value The index number of a variable in the symbol table of a Ch program If the name is not in the list of the symbol table the function returns 1 Parameters interp A Ch interpreter name The symbol name of a global variable in a Ch program Description The function Ch_SymbolIndexByName obtains the index number of a variable in the symbol table in a Ch program inside a dynamically loaded library or embedded Ch program If the control flow of the Ch program is inside a function the symbol table contains local variables including function arguments within its scope inside the function If the control flow of the Ch program is inside a member function of a class the symbol table contains local variables including members of the class and function arguments within its scope inside the function If the control flow of the Ch program is outside a function the symbol table contains global variabl
72. Ch script invoked by Embedded Ch has its own search pathes for commands include header files and function files specified by the systems variables path _ipath and fpath respectively The value for the system variable path is also copied to the environment variable PATH The pathes specified by the environment variable PATH are searched for dynamically loaded libraries in Windows If your application needs some dynamically loaded libraries which are not located in the default directories specified by the system variable path make sure they are added to path in the system startup file EMBEDCHHOMB config chrc or individual user s startup file chre in the user home directory If some dynamically loaded libraries are located in C myApplication bin add the statement below in the startup file path stradd fpath C myApplication bin 5 Function files with extension chf used in Ch scripts can be distributed along with your application You may put these function files in a separate directory under your application home directory such as C myApplication ch lib Then add the directory in the system variable fpath in file config chrc under the home directory of Embedded Ch as follows _fpath stradd fpath C myApplication ch lib Similarly the search paths C myApplication ch include and C myApplication ch dl for header files and dynamically loaded libraries with file extension dl in Ch respectively can be added as fol
73. Ch_RunScript performs these two steps together Often time it is desirable to check the syntax of the embedded Ch program before it is executed This can be accomplished by functions Ch ParseScript and Ch_ExecScript as shown in Program 1 4 Function Ch_ParseScript parses a Ch program only Later the parsed program can be executed through function Ch ExecScript The arguments of function Ch ParseScript is the same as those of function Ch RunScript The function returns 0 if the program is parsed successfully Otherwise it returns non zero Like other functions the first argument of function Ch ExecScript is Ch interperter The second argument is the first element of the second argument of array of pointer to char in function Ch ParseScript The function returns 0 on successful and 1 on failure Binary executable C C programs can call Ch functions which are subject to change at the user s preference In Program I 1 the second argument of function Ch Initialize is NULL to indicate the CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 3 OTHER ESSENTIAL APIS AND OPTIONS FOR EMBEDDED CH KK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kok k k k k k k k kck k ck k kok ck ck kok ko K File Name embedchlb c executing Ch program in C program with embedded Ch using separate parse and execution functions OKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCk Ck k Ck k ck k k k kk kk k k k
74. Chapter A APls for Embedding Ch lt embedch h gt Ch SetGlobalUserData sp struct tag userdata x Ch VaArg interp ap double retval func sp x Ch VaEnd interp ap return retval int main ChInterp t interpl interp2 struct tag sl 10 20 0 struct tag s2 100 200 0 h_Initialize amp interpl NULL set the global user data for the 1st Embedded Ch engine interpl h SetGlobalUserData interpl ChPointer t amp s1 h DeclareFunc interpl int func double x ChFuncdl t func chdl h ExprEval interpl func 50 GA AA S A h_Initialize amp interp2 NULL set the global user data for the 2ns Embedded Ch engine interp2 h SetGlobalUserData interp2 ChPointer t amp s2 h DeclareFunc interp2 int func double x ChFuncdl t func chdl h ExprEval interp2 func 500 Aaa a close interpl and interp2 Ch End interpl Ch End interp2 return 0 Output sp gt i 10 sp gt d 20 000000 x in func 50 000000 sp gt i 100 sp gt d 200 000000 x in func 500 000000 Example 2 CHHOME toolkit demos embedch chapters appendix threadswindata c is an example that il lustrates how to share the global data in different Ch scripts running in a multi thread binary C program in Windows Example 3 CHHOME toolkit demos embedch chapters chapter1 threadsync c program starts 4 threads Each thread has its own Ch int
75. ERROR printf Error Ch CallFuncByName failed n exit 1 status Ch_CallFuncByName interp callback NULL otherFunc list Ch End interp void printlist char list int i printf list in printlist in C is p n list fort ie0s Dist i z I printf list d Ss n i list i this function is called from the Ch space EXPORTCH void callback chdl void varg ChInterp t interp ChVaList t ap void callback char char ALESE Ch VaStart interp ap varg callback Ch VaArg interp ap FUNCPTR list Ch VaArg interp ap char printf list in callback chdl in C is p n list now we call the real function callback list Ch VaEnd interp ap Program 5 18 A C program calling backo C function from a Ch function prog2 c CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File embedch2 ch include lt stdio h gt include sample h this prototype will not work because of the internal checking void callback void func char char list void callback void func char list void fptr fptr dlsym Chsample handle callback chdl if fptr NULL fprintf stderr Error s dlsym s n X func dlerror return printf list in callback in Ch is p Mn list dlrunfun fptr NULL callback func list Program 5 19 Ch pr
76. Example 1 Use binary functions in the C space as system functions in the Ch space File Name declarefunc c include lt stdio h gt include lt embedch h gt struct tag int i p function of void type void funcl double x printf x in funcl is f n x function with a return value double func2 double x double y printf x in func2 is f n x printf y in func2 is f n y return xty function with arguments of struct and pointer to struct returning a pointer to struct struct tag func3 struct tag sp struct tag s printf sp gt i in func3 is d n sp gt i printf s i in func3 is dWMn s i sp gt i s i return sp 237 Chapter A APIs for Embedding Ch lt embedch h gt SPECIAL CASE for function returning a struct similar to the method described in Ch SDK struct tag func4 int i struct tag s struct tag s2 s2 Ss printf s i in func4 is dWMn s i S2 i i return s2 SPECIAL CASE for function with variable number of arguments int vfunc5 int n va list ap int a double d i va arg ap int d va arg ap double printf i in vfunc5 printf d in vfunc5 return 2 n Sdin i si n d int Lunes lint ny ss 4 va_list ap int retval va start ap n retval vfunc5 n ap va end ap return retval EXPORTCH void funcl chdl void varg
77. In this case the datatype is ignored The data type of the result shall be compatible with the data type of the expression Example In this example there are two global variables in program exprcalc ch iiis an integer and dd is a value of double type We can calculate the expression dd dd ii from C space by function Ch ExprCalc and get the new value of dd by function Ch SymbolAddrByName The result can also be obtained by passing the address for the result in the third argument of function Ch ExprCalc We can call the Ch function un through evaluating expression fun in exprcalc c chexprcalc c A C program to calculate expressions in Ch address space BORK kk kk kk kk kk kk kk kk KCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCk kk kck kk k k k k ck k k Ck ck ck ck ok kk ke kk Calculate an expression and function in Ch address space OKCKCKCKCKCKCKCKCKCkCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCKCKCkCKCkCKCkCKCk Ck k ck k ck kk kk ck ck ck ck ck ck ck ck ok ok Kok ke ke 248 Chapter A APIs for Embedding Ch lt embedch h gt include lt stdio h gt include lt ch h gt EXPORTCH void chexprcalc chdl void varg ChInterp t interp ChVaList t ap int pii double pdd double result Ch VaStart interp ap varg pii Ch SymbolAddrByName interp ii pdd Ch SymbolAddrByName interp dd printf ii d n pii printf dd f n pdd if Ch_ExprParse interp fun Ch_Exp
78. LIB C Program Files Microsoft Visual Studio 8 VC lib C Program Files Microsoft Visual Studio 8 VC PlatformSDK lib C Program Files Microsoft Visual Studio 8 SDK v2 0 lib getenv LIB putenv stradd INCLUDE C Program Files Microsoft Visual Studio 8 VC include C Program Files Microsoft Visual Studio 8 VC PlatformSDK Include C Program Files Microsoft Visual Studio 8 VC atlmfc include getenv INCLUDE If a VC in NET 2003 is installed at C Program Files Microsoft Visual Studio NET 2003 the code below needs to be added to the startup file chrc in the user s home directory _path stradd _path C Program Files Microsoft Visual Studio NET 2003 VC7 bin path stradd path C Program Files Microsoft Visual Studio NET 2003 COMMON7 IDE putenv stradd LIB C Program Files Microsoft Visual Studio NET 2003 VC7 lib C Program Files Microsoft Visual Studio NET 2003 VC7 PlatformSDK lib getenv LIB putenv stradd INCLUDE C Program Files Microsoft Visual Studio NET 2003 VC7 include C Program Files Microsoft Visual Studio NET 2003 VC7 PlatformSDK Include C Program Files Microsoft Visual Studio NET 2003 VC7 atlmfc include getenv INCLUDE For VC 6 0 installed at C Program Files Microsoft Visual Studio the code below needs to be added to the startup file _chrc in the user s home directory string t tmp path stradd path C Program Files Micros
79. SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE include lt stdio h gt finclude ch h finclude sampclass h Class1i Class1 mil 1 printf m il in Classl Classl d n m i1 Classi Class1 printf m il in Classl Class1 dWMn m i1 int Class1 memfunl int i m il i printf m il in Classl memfunl dWMn m i1 return m il Program 5 40 C class member functions file sampclass cpp target prog exe prog exe prog o sampclass o ch dllink prog exe cplusplus embedded prog o sampclass o prog o prog cpp ch dlcomp libclass dl cplusplus prog cpp sampclass o sampclass cpp ch dlcomp libclass dl cplusplus sampclass cpp clean rm o dl exe Program 5 41 Makefile to create prog exe for Unix Makefile exe target prog exe prog exe prog obj sampclass obj ch dllink prog exe cplusplus embedded prog obj sampclass obj prog obj prog cpp ch dlcomp libclass dl cplusplus prog cpp sampclass obj sampclass cpp ch dlcomp libclass dl cplusplus sampclass cpp clean rm obj dl exe lib Program 5 42 Makefile to create prog exe for Windows Makefile exe Unlike the example in the previous section the handler g sample handle for the dynamically loaded 112 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE module is obtained by the programming statement
80. Then the chdl file shown in Program 5 7 in turn calls the binary C functions The macro in Program 5 7 Ch VaStart interp ap varg 80 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE ifndef SAMPLE H define SAMPLE H funcl and func2 use sin and hypot in math h include lt math h gt in C Ch space extern double funcl double x extern double func2 double x double y ifdef CH include lt dlfcn h gt void _Chsample_handle ifndef HPUX _Chsample_handle dlopen NULL RTLD LAZY if Chsample handle NULL fprintf stderr Error dlopen s n dlerror fprintf stderr cannot get Chsample handle in sample h n exit 1 void _dlclose_sample void dlclose Chsample handle atexit dlclose sample fendif fendif endif SAMPLE H Program 5 4 C Ch header file sample h 81 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE double funcl double x void fptr double retval fptr dlsym Chsample handle funcl chdl if fptr NULL fprintf stderr Error s dlsym s n X funoc dlerror return NaN dlrunfun fptr amp retval funcl x return retval Program 5 5 Ch function file funcl chf double func2 double x double y void fptr double retval fptr
81. a and ext b are also passed to the Ch function by Ch CallFuncByName The arguments aa1 and bb1 are only addresses in C space and dim ext a and ext b will provide the information about the extents of arrays aal and bbl to the Ch function For the same reason if Ch function funct takes an argument of X dimensional assumed shape array where X can be any integer the users should add X extra arguments in Ch CallFuncByName to provide the information of extents of all dimensions of the array Since the Ch function funct only takes assumed shape arrays of fixed dimension as arguments the users don t need to provide the information of dimensions in C space A complete sample of this case is shown in Programs 6 T and In Ch program callchvla2d ch shown in Program 6 1 the function sum2d is designed to calculate the sum of each element of a two dimensional assumed shape array a The function shape can be used to get the information of shape of an array in Ch space In C program callchvla2d c sum2d is called twice by Ch CallFuncByName to calculate the sum of arrays of different shapes The array aa1 has shape of 2 x 3 In the code below dim 2 extl 2 ext2 3 dim and extents of aal Ch CallFuncByName interp sum2d amp sum ret aal dim extl ext2 117 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt array h gt doub
82. a Ch program Return Value The address for the symbol name of a global variable in a Ch program If the index number is out of the valid range for its symbol table the function returns NULL Parameters interp Ch interpreter num The index number of global variable in the symbol table in a Ch program Description Based on its index number of a global variable in the symbol table the function Ch GlobalSymbolNameByIndex obtains the address for the symbol name of a global variable inside a dynamically loaded library or embedded Ch program The index number for the symbol table starts with 0 Example See Ch GlobalSymbolAddrByIndex See Program 2 4 See Also Ch GlobalSymbolTotalNum Ch_GlobalSymbolIndexByName Ch_GlobalSymbolAddrByName Ch_GlobalSymbolAddrByIndex Ch_SymbolNameByIndex 281 Chapter A APIs for Embedding Ch lt embedch h gt Ch GlobalSymbolTotalNum Ch GlobalSymbolTotalNum Synopsis include lt ch h gt int Ch GlobalSymbolTotalNum ChlInterp t interp Purpose Get the total number of global variables in the symbol table in a Ch program Return Value The total number of global variables in the symbol table in a Ch program Parameters interp Ch interpreter Description The function Ch GlobalSymbolTotalNum obtains the total number of global variables in the symbol table inside a dynamically loaded library or embedded Ch program The symbol table contains user defined functions b
83. a variable CH GLOBALVARTYPE a global variable CH LOCALVARTYPE a local variable Portability This header file has no known portability issues A 2 Relevant Information in Header File ch h The header file ch h is included in the header file embedch h The detailed information about header file ch h is presented in an appendix in Ch SDK User s Guide In this section some relevant information in header file ch h is highlighted Data Types The following data type are defined in the header file ch h Data Type Description ChlInfo t Information for Embedded Ch obtained by Ch Version ChInterp t A Ch interperer The first argument for all Ch SDK APIs ChType t The internal Ch data type ChVaList t A Ch variable number of arguments in the C space Functions The following functions defined inside the header file ch h are often used for embedding Ch into applica tions Function Description Ch CallFuncByAddr Call a Ch function by its address from the C address space Ch CallFuncByAddrv Call a Ch function by its address from the C address space Ch CallFuncByName Call a Ch function by its name from the C address space Ch CallFuncByNamev Call a Ch function by its name from the C address space Ch CallFuncByNameVar Call a Ch function by its name from the C address space with a variable number of arguments Ch GlobalSymbolAddrByNameGet the address of a global variable based on its name Ch Home Get the home directory in which Emb
84. be found in the directory CHHOME toolkit demos Windows The output from standard streams of a script can also be passed to the host application first then dis played In this case functions Ch Reopen Ch_Close and Ch Flush with the following function prototypes can be used to handle redirection of stdin stdout and stderr streams int Ch Close ChInterp t interp ChFile t fildes ChFile t Ch Reopen ChInterp t interp const char filename const char mode int fildes int Ch Flush ChInterp t interp ChFile t fildes where data type ChFile t for file descriptor is defined in header file embedch h File descriptors STDIN FILENO STDOUT FILENO STDERR FILENO corresponding to stdin stdout and stderr streams respectively defined in the header file embedch h can be used as the fourth argument of function Ch Reopen In some platforms function Ch Reopen need to be called before any I O happens It is best to call function Ch Reopen right after Ch Initialize is called as shown in in Program 12 In Program 1 12 the argument argvv of function Ch ParseScript for program name reopen ch without command line option is created dynamically Before script reopen ch in Program 1 13 is parsed by function Ch ParseScript in Embedded Ch the stderr stream is redirected to a temporary file stderrfile by function call fildes stderr Ch Reopen interp stderrfile w STDERR FILENO which is equivalent to FILE str
85. begining of a function call if calldata gt event amp CH MASKCALL printf event MASKCALL is true n if calldata gt event amp CH_MASKRET printf event MASKRET is true n if calldata gt event amp CH MASKBLOCK printf event MASKBLOCK is true n if calldata gt event amp CH MASKEND printf event MASKEND is true n if calldata gt event amp CH MASKLINE printf event MASKLINE is true n if calldata gt event amp CH MASKCOUNT printf event MASKCOUNT is true n if calldata gt event amp CH MASKABORT printf event MASKABORT is true n if calldata gt event amp CH MASKCOUNT printf The count number is d n calldata gt count printf The stack level is d n calldata gt level printf The current line number is d n calldata gt linecurrent Program 7 2 C program with a callback function progl c 132 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION if calldata gt classname printf Member function s s starts at line d Mn calldata gt classname calldata gt funcname calldata gt linefuncbegin printf Member function s s ends at line d Wn calldata gt classname calldata gt funcname calldata gt linefuncend printf Member function s s Mn calldata gt classname calldata gt funcname printf Class name i
86. ch Function main at stack level 2 has 3 symbols func main T 10 x 0 The stack level is 3 The current line number is 0 The current file name is nestedfunc ch Program nestedfunc ch at stack level 3 has 4 symbols i 40 g 100 0000 nestedfunc 0x5a770 main 0x5d718 Figure 7 8 Output from executing prog4 nestedfunc ch 160 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES 7 3 2 Changing Values of Variables Many development tools such as a debugger need to change the value of a variable or evaluate an expression during the execution of a script In this section a sample program will be used to illustrate how to change the value of a variable in a script and evaluate a Ch expression in the callback function Program 7 1l is used to process the script in Program 7 12 Program 7 12 is modified from Program 7 1 Program 7 12 contains some printing statements to view the values of variables in the Ch space changed in a callback function The output from executing Program 7 11 is shown in Figure Z9 The callback function in Program is triggered similarly as in previous examples The block end event in the if statement in function func1 in Program 7 12 allows the compound statements within the if statement in the function callback executed It first prints out the stack information for the active function using function printCalldata as presented
87. ch in interpl failed n initialize another embedded Ch interp2 Ch Initialize amp interp2 NULL execute embedch2 ch status Ch RunScript interp2 argv2 if status CH ERROR printf Error execution of embedch2 ch in interp2 failed n use variables and functions in embedch2 ch in interp2 Ch CallFuncByName interp2 fun amp retval 10 release memory and terminate interperters Ch End interpl Ch End interp2 Program 1 9 A program executing multiple embedded Ch programs using multiple instances of Ch inter preter thread c Program I 9 illustrates how to invoke multiple Ch interpreters embedded in an application with a single thread In this example two function calls Ch Initialize initialize two instances interpl and interp2 of Ch interpreter The same Ch interpreter interpl executes both Ch programs embedch1 ch and embedch2 ch by calling function Ch RunScriptM twice Global variables in embedded Ch pro grams embedch1 ch and embedch2 ch are in separate namespaces There is no memory collisions between embedch1 ch and embedch2 ch The global variables in these Ch programs executed by Ch RunScriptM however cannot be accessed later in the host program Global variables for the Ch program embedch2 ch loaded by different interpreters are also in different namespaces The instance of Ch interpreter interp2 executes the Ch program embedch2 ch using Ch RunScript
88. class type function type etc The type qualifier decl spec global is added at the beginning of the declaration statement of function Ch DeclareVar The statement Ch DeclareVar interp int a b is equivalent to declare system variables a and b in the Ch space as follows declspec global int a b Because type specifiers declspec global and typedef cannot be used together to declare a variable Ch_DeclareVar cannot be used to define a new data type with t ypedef as shown below Ch DeclareVar interp typedef int newtype t The new data type for a system variable can be created by the function Ch DeclareTypedef as shown below Ch DeclareVar interp int newtype t Ch DeclareTypdef interp newtype t Ch DeclareVar interp newtype t sysvarl In this case Ch DeclareVar interp newtype t sysvarl is equivalent to declspec global int sysvarl in the Ch space Example See Ch DeclareFunc See Also Ch DeclareVar Ch DeclareFunc Ch AppendParseScript Ch AppendRunScript 241 Chapter A APIs for Embedding Ch lt embedch h gt Ch_Declare Var Ch_DeclareVar Synopsis include lt embedch h gt int Ch_DeclareVar ChInterp_t interp const char declaration Purpose Declare system variables in the Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter declaration A declaration statement including
89. const char argv Purpose Execute a Ch program from C space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp Ch interpreter argv File name and arguments of the Ch program The last field must be NULL Description When a program is run in Ch it first will be parsed then executed Ch RunScript performs these two steps together The function Ch RunScript executes a Ch program from C space The variable argv contains the file name and arguments of the Ch program to be executed It must end with NULL in the last element of the variable The API Ch Initialize should be called before this function is called After function Ch RunScript is called variables and functions inside the script can be accessed by other APIs in the same interpreter interp However after function Ch RunScriptM is called variables and functions inside the script cannot be accessed by other APIs in the same interpreter interp Example Refer to Program 1 1 See Also Ch AppendParseScriptO Ch AppendRunScriptO Ch ParseScript Ch ExecScript Ch ExecScriptM Ch RunScriptM Ch Initialize Ch Home Ch SymbolAddrByName Ch DataType Ch ExprEval Ch End 291 Chapter A APIs for Embedding Ch lt embedch h gt Ch RunScriptM Ch RunScriptM Synopsis include lt embedch h gt int Ch RunScriptM ChInterp t interp const char argv Purpose Execute a Ch program from C spac
90. dlsym Chsample handle func2 chdl if fptr NULL fprintf stderr Error s dlsym s n X func dlerror return NaN dlrunfun fptr amp retval func2 x y return retval Program 5 6 Ch function file func2 chf initializes a Ch interpreter interp of type ChInterp_t and an object ap of type ChVaList t to represent in the C space for a variable number of arguments in the Ch space for subsequent use by macro Ch VaArg and function Ch VaEnd Macro Ch VaArg obtains arguments passed from the function dlrunfun in a function file These macros and functions are defined in the header file ch h and described in detail in Ch SDK User s Guide Function files in Programs 5 5 and 5 6 should be placed in one of directories specified by the system variable _fpath for function file pathes as described in section L7 Function files shown in Programs 5 5 and and C wrapper file shown in Program 5 7 can be created automatically from the header file sample h by using utility program c2chf and functions processfile and processcfile as described in Chapter 3 of Ch SDK User s Guide 82 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE include lt ch h gt include lt sample h gt EXPORTCH double funcl chdl void varg ChInterp t interp ChVaList t ap double x double retval Ch VaStart interp ap varg x Ch VaArg interp ap do
91. eux monde qw VR ukon Bah Y ELE E UR de zao EO ES 247 BE T L eee 248 KO O O O oo ENNIUS 251 E O P O 254 Eb gestae SR PROV iene nos as oe eee OVO O O 255 DIM OOo 256 V O O a 257 Ch FuncAreArravExtenl 2222 258 T 259 V O O ee 260 ME 261 Sieh o ee ead ee oO n Oo a 269 ZE 270 Se Oe Ged do oe BA Bala alo a ee U 271 P 272 EPA 273 AP OR ee eee Se E 274 Ch FuneTvpd RR s 275 Sf esas Hee BA Sle ode a es ee ees ees 276 QUUM 277 r E ASSI EV MIS 280 hGlobalSvmbolNameBvIndex ss 281 M OE ELE O OOP 282 CUM 283 F 286 E O O O OR O 288 SPO U 6 Re O O eee 289 Ch HS vy daa tue cio Bea at Suge a eae TILES 290 humor sce s ao CA aw oS oe RRS bln bem Ghd ed wh bebe ad whee 291 ChRunScriptM o oaa 292 SPT a a a an i ae ORE 293 EA ooa RH AAA A DR ERGY GRR DS 295 CMM 299 a 301 ER O POE Sah aa SEES O R 303 Pus M O O L bak 305 P KO a ee 306 Ch SymbolNameByIndex lt 2 oaa aaa a a B 1 Porting umm with Embedded Ch APIs to Ch Version 6 0 B 2 Porting Code with Embedded Ch APIs to Ch Version 5 5 viii 322 ix Chapter 1 Embedding Ch Programs in C Space Note The source code and templates for all examples described in this document are available in CHHOME toolkit demos embedch chapters and CHHOME toolkit demos embedch appendix where CHHOME is the directory where Ch is installed such as C Ch for Windows and usr local ch for Unix For Windows some additional examples are available at and C
92. executing Program 2 6 is shown in Figure 2 4 34 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 3 OBTAINING INFORMATION FOR A VARIABLE OF STRUCTURE CLASS AND UNION ch i 10 ch i 10 ch p 20 c i 30 ch data 9 10 Figure 2 3 Output from executing setvar c 2 3 Obtaining information for a Variable of Structure Class and Union Ch provides the following four APIs to find information for user defined variables of structure class and union types ChUserDefinedTag t Ch UserDefinedTag ChInterp t const char name int Ch UserDefinedInfo ChInterp t interp ChUserDefinedTag t udtag ChUserDefinedInfo t udinfo int Ch UserDefinedMemInfoByName ChInterp t interp ChUserDefinedTag t udtag const char memname ChMemInfo t meminfo int Ch UserDefinedMemInfoByIndex ChInterp t interp ChUserDefinedTag t udtag int index ChMemInfo t meminfo A user defined structure class and union has a tag This tag can be obtained by the function Ch UserDefinedTag based on the name of variable or tag name The returned tag from function Ch UserDefinedTag is passed as the second argument of function Ch UserDefinedInfo The infor mation for the user defined data type is passed back to the calling function in the third argument of function Ch UserDefinedInfo The ChUserDefinedInfo_t structure contains three members for the tag name size and total number of members of the user defined type The info
93. first function call n printf pretval g d n pretval g Ch ExprEval interp ii Ch ExprEval interp dd dd ii Ch ExprEval interp retval g func ii call Ch function func printf In C after expression evaluations and the second function call n printf pretval g d n pretval g Ch End interp Program 3 1 Calling Ch function by expression evaluations funcall c 48 CHAPTER 32 C amp ALINNGHIFENNCIOSN ROM OBRKS USING CH CALLFUNCBYADDRO AND CH CALLFUNCBYADDRV int ii 10 double dd 1 1 int retval g int func int argl printf Xnprinted from Ch function func ii Sd dd f n ii dd return 2 argl Program 3 2 Calling Ch function by expression evaluations funcall ch In C the original value of pretval_g pretval_g 0 printed from Ch function func ii 10 dd 1 100000 In C after the first function call pretval g 20 printed from Ch function func ii 11 dd 12 100000 In C after expression evaluations and the second function call pretval_g 22 Figure 3 1 Output from executing funcall c Ch ExprEval interp retval g func ii to print out the new values of these two Ch variables To get the return value of the function call a global variable for example retval g is used as the Ivalue of an assignment operation to save the return value of the Ch function Then it can be retrie
94. for a structure class union definition and local variables of automatic duration not in its scope The information about Program 2 3 is processed by Program The output of Program 2 4 is displayed in Figure 2 2 The total number eight of all symbols in the symbol table is obtained by function Ch SymbolTotalNum The variable d is the second symbol in the symbol table as shown in the return value from function Ch_SymbolIndexByName Note that the index number for a symbol table starts with 0 The address of a variable can be obtained by function Ch SymbolAddrByName or Ch SymbolAddrByIndex based on its name or index num ber in the symbol table respectively The address for a function prototype without function definition or an extern variable without definition such as prototype and e respectively in Program 2 3 is also NULL The symbol name corresponding to its index number in the symbol table can be obtained by func tion Ch SymbolNameBylIndex 31 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 1 ACCESSING VARIABLES BY ADDRESSES KKK kA kA kA kA kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk k ck k Ck k k k k kok kok kok kok kok File Name symbol c OKCKCKCKCKCKCKCKCKCKCKCkCKCKCKCKCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCkCKCkCKCk Ck k ck k ck k k ck kck ck kk k k ke ke ke e x include lt stdio h gt include lt embedch h gt int numOfVar ChInterp t interp int index totalnum varnum 0 char name totalnum Ch
95. func amp retval g 20 call Ch function func printf In C after expression evaluations and the second function call n printf retval g d n retval g Ch End interp Program 3 5 Calling Ch function with API Ch_CallFuncByName funcall3 c 52 CHAPTER 3 GALOMG ONCFON CURSING PROM ISRAGEUSING CH CALLFUNCB YNAME AND CH CALLFUNCBYNAMEVO C Space funcall exe I a VS Ch Space i main I funcall2 ch D Ch Initialize l func lt Ch RunScript Ch ExprEval interp func I Ch CallFuncByName interp func I ED i V LL 4 Ch End I i Figure 3 3 Calling functions in Ch program will be listed following the third argument retval The function returns zero on successful execution or non zero on failure C program funcall3 c in Program is an example of calling Ch functions with API Ch_CallFuncByName Program B 4Jis used as the Ch program in this example Figure 3 3lillustrates how to call a Ch function func by expression evaluations and API Ch CallFuncByName After the Ch program is loaded and executed the statement below Ch CallFuncByName interp func amp retval g 10 invokes Ch function func by its name It is equivalent to statements fptr Ch SymbolAddrByName interp func Ch CallFuncByAddr interp fptr amp retval g 10 in Program 3 3 The output from executing the file bui
96. in stack c static functions within the file static int printValue ChInterp t interp ChType t datatype void addr ChUserDefinedTag t udtag int nestlevel static int printUserDefined ChInterp t interp ChType t datatype void addr ChUserDefinedTag t udtag int nestlevel extern void chPrintSymbolValue ChInterp t interp char name void addr extern void chPrintSymbolValuesInStack ChInterp t interp int level functions in callback c extern void chCallback ChInterp t interp ChBlock t calldata ChPointer t clientdat a extern int chTestBreakPointCondition ChInterp t interp breakPoint t bp functions in menu c the console can be replaced by a GUI extern int chHelp void functions in debug c static functions within the file static action t getActionMenu debugInfo t dgptr char str static void printErrorNoFile void static int getArgvFromStr char argv char args extern in hInitDebug debugInfo t dgptr hEndDebug debugInfo t dgptr hCleanDebug debugInfo t dgptr hLoadFile debugInfo t dgptr char progname hStart debugInfo t dgptr runmode t runmode char args hRun debugInfo t dgptr char args hStepInto debugInfo t dgptr hStepOver debugInfo t dgptr hContinue debugInfo t dgptr hUpStack debugInfo t dgptr hDownStack debugInfo t dgptr hStack debugInfo t dgptr hLocals debugInfo t dgptr hVariables debugInfo t dgptr
97. in the C space is pushed into the Ch space as an argument func in the function callback Note that since the argument func is a pointer to function in the C space and shall not be called directly in the Ch space it is treated as a variable of pointer to void It cannot be declared as a pointer to function in Ch space This pointer to function is passed back to the wrapper function callback chdl When this callback function is invoked in the function callback chdl it actually calls the function printlist in the C space passed from an argument of the function Ch_CallFuncByName Different callback functions can be pushed into the Ch space by the function Ch CallFuncByName Unlike function printdata in Programs 5 15 function printlist has no return value It illustrates how arrays of string are handled through its argument The output from execution of Program 5 T8 is displayed in Figure 5 3 The source code for examples described in this section are available in the directory CHHOME toolkit demos embedch chapters chapter5 callback The C functions can be defined using Ch DeclareFunc as system functions in the Ch space as de 88 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File progl c include lt stdio h gt include lt string h gt include lt embedch h gt include sample h double data 4 10 0 20 0 30 0 40 0 int main int arg
98. include lt string h gt include debug h if defined WIN32 define R OK 4 define F OK 0 else include lt unistd h gt endif static functions debug eX Test for Read permission Test for existence of File within the file static void printErrorNoFile void static action t getActionMenu debugInfo t dgptr static int getArgvFromStr char argv int chInitDebug debugInfo t dgptr char proghome ChOptions t option setbuf stdout NULL setbuf stderr NULL dgptr gt progname NULL memset dgptr gt argvv N0 MAX dgptr gt interp NULL To use your distribution of option option gt shelltype CH REGULARCH proghome getenv PROG HOME or obtain the value using Windows API option gt chhome trcpy option S strcat option dgptr gt option tr option tr callback tr level tr runmode tr bkpt RegOpenKey Embedded Ch ChOptions t malloc sizeof ChOptions t Ex malloc s proghome char gt chhome tr clientdata tr count 0 0 tr gt wexpr NU gt chhome embedch option NULL chCallback Ch RUNMOD E UNDEFINED NULL LL turn 0 chl if dgp int tr stepovernum 0 EndDebug debugInfo t dgptr tr gt interp Ch End dgptr gt interp chCleanDebug dgptr return 0
99. interp newtype t Both global variables and system variables can be removed by a pragma statement as shown below pragma remvar a pragma remvar b 242 Chapter A APIs for Embedding Ch lt embedch h gt Ch DeclareVar in the Ch space which can accomplished using Embedded Ch by Ch AppendParseScripl Ch AppendParseScript t interp t interp pragma remvar a pragma remvar b An identifier can be declared as both global and system variable In this case the identifier will be treated as a global variable inside a program For a variable of function type only one function definition can be defined if the identifier is used for both global and s A struct type used as a function argument or Ch_DeclareFunc can be defined by Example ystem variable return type of a system function defined by function Declare and use system variables of different data types in the Ch space File Nam declarevar c include lt stdio h gt include lt embedch h gt int main ChInterp t interp int status int c i initialize embedded Ch Ch Initialize amp interp NULL declare variable ch i in Ch space Ch DeclareVar interp int ch i declare and initialize variables c Ch DeclareVar interp int ch j 10 c Ch DeclareVar interp int ch p amp ch_ declare struct Ch DeclareVar interp struct tagl int declare function Ch DeclareVar
100. interp A Ch interpreter event The events in which the callback function will be called clientdata The data passed to the callback function callback The callback function which will be called when an event occurs count The callback function which will be called after Ch executes every count instructions This argu ment is meaningful only when the event contains the event mask Ch MASKCOUNT Description Function Ch_AddCallback adds a callback function in the third argument of function Ch_AddCallback The second argument event specifies on which events the callback will be called The event masks below can be used to specify events when the callback function is called It can also be used to obtain the event inside a callback function to determine which event triggered the callback function Macro Description Ch MASKNONE No callback Ch MASKCALL Just after Ch calls and enters a new function Ch MASKRET Just before Ch leaves the function Ch MASKBLOCK Just after Ch enters a new block Ch MASKEND Just before Ch leaves the block Ch MASKLINE When Ch is about to start the execution of a new line of code or when it jumps back in the code even to the same Ine Ch MASKCOUNT After Ch executes every count instructions Ch MASKABORT When Ch is aborted by Ch Abort 210 Chapter A APIs for Embedding Ch lt embedch h gt Ch AddCallback The event mask Ch MASKCOUNTalong with the fifth argument count of function Ch AddCallback
101. is 6 symbol index 0 name i address 58d78 symbol index 1 name z address 58da8 symbol index 2 name _ func address 553d0 symbol index 3 name g address 58ce0 symbol index 4 name y address 58d10 symbol index 5 name i address 59c50 i 30 UT BUN The stack level is 1 The current line number is 19 Function nestedfunc starts at line 4 Function nestedfunc ends at line 20 The function name is nestedfunc The current file name is nestedfunc ch The total number of symbols is 5 symbol index 0 name _ func address 3e918 symbol index 1 name g address 59b40 symbol index 2 name r address 59b70 symbol index 3 name funcl address 50730 symbol index 4 name i address 50198 i 10 rhe stack level is 2 The current line number is 25 Function main starts at line 22 Function main ends at line 27 The function name is main The current file name is nestedfunc ch The total number of symbols is 3 symbol index 0 name _ func address 4f870 symbol index 1 name i address 5b0b8 symbol index 2 name x address 5b0e8 i 10 The stack level is 3 The current line number is 0 The current file name is nestedfunc ch The total number of symbols is 4 symbol index 0 name i address 44f90 symbol index 1 name g address 53750 symbol index 2 name nestedfunc address 59828 symbol index 3 name main address 5c7d0 i 40 Figure 7 4 Output from executing prog2 nestedfunc ch
102. is added before the script is executed by the function Ch RunScript The events that trigger the callback function are function call event function return event entering a block event and leaving a block event They are specified by the statement mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND Program 7 1 calls two functions main and funcl and execute a block inside an if statement There fore the callback function will be triggered and executed six times In this case the count argument of Ch AddCallback is is irrelevant The contents for the variable s of structure t ag is passed as the client data in the fourth argument of the function Ch AddCallback This client data is passed to the callback function callback as the third argument The members of the structure are printed out when the callback function is triggered by a call event for the function main using the following code H f calldata gt event amp CH MASKCALL amp amp strcmp calldata gt funcname main struct tag sp 130 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION int i 40 double g 100 double a 5 1 2 3 4 5 void funcl int i double g 200 int y printf funcl is called n if i int i 20 z i 30 printf argument i in funcl is true n int main int i 10 x funcl i return
103. it is NULL Field classname contains the class tag name if the executed line of code is within a member function of a class Otherwise it is NULL Field isconstructor is 1 if the executed line of code is within a constructor of a class Otherwise it is 0 213 Chapter A APIs for Embedding Ch lt embedch h gt Ch AddCallback Field isdestructor is lI if the executed line of code is within a destructor of a class Otherwise it is 0 Example See Program See Also Ch ChangeStack 214 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendParseScript Ch AppendParseScript Synopsis finclude lt embedch h gt int Ch AppendParseScript ChInterp t interp const char code Purpose Append a fragment of the Ch code to an existing Ch program inside the interpreter executed by functions Ch RunScript or Ch ParseScript and Ch ExecScript The Ch code will be parsed without execution Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter code A fragment of the Ch code to be appended to the existing Ch program inside the interpreter Description When a program is run in Ch it first will be parsed then executed The process is similar to compile and execution using a C compiler Like Ch RunScript But Ch AppendParseScript parses the code only The code is not executed Functions Ch ExprEval Ch CallFuncByAddr Ch CallFuncByName or Ch_Ca
104. lib 6 ChType t ChUserDefinedInfo t dtype size tagname totnum ChUserDefinedMemInfo_t arraytype dim dtype extent fieldoffset fieldsize index isbitfield isconstructor isdestructor isfunc 201 ismemberfunc ispublic 324 isvararg memname offset udtag 201 312 ChUserDefinedTag t ChVaList t ChValueNode t ChVarType t 099 203 B 16 copyright data type 210 228 229 26 2691275 299 BOL 309 ws 14H31 ION DLL dlopenQ I dirunfun 0 disym I double 229 261 Dynamically loaded library I embedch lib 6 8 embedch_bc lib 9 embedch_mdd lib 6 embedch_mt lib 6 embedch mtd lib 6 EXPORTCH EXPORTCHCLASS expression calculation 248 expression evaluation expression parse 254 fixed length array 116 function files generic function 31 getenv 6 global variable 45 INCLUDE 6 8 int Intel C compiler 8 LIB 6 8 macros CH ABORT 205 CH DOUBLETYPE 229 261 CH_ERROR 205 CH_INTTYPE 229 D61 3 1 I 1 Chapter B APIs for Embedding Ch lt embedch h gt CH INVALIDLEVEL CH MASKABORT CH MASKBLOCK CH MASKCALL CH MASKCOUNT CH MASKEND CH_MASKLINE 203 210 CH MASKNONE 203 210 CH MASKRET CH OK CH REGULARCH 11 203 286 CH SAFECH 11 203 286 STDERR FILENO 22 203 290 STDIN FILENO 22 203 290 STDOUT_FILENO 22 2031 290 makefile multi
105. locals displays names and values of variables in the current scope by calling function chLocals which in turn calls the function chPrintSymbolValuesInStack in Program 7 9 described in the previous section When the con trol of the program execution is inside a function command locals displays the values of local variables and arguments of the function When the control of the program execution is not in a function of a script command locals displays the values of global variables of the program Function chVariales corresponding to command variables displays names and values for all variables within its scope in each stack recursively by calling function chPrintSymbolValuesInStack The command watch calls function chAddWat chExpr to add an expression to a list of watched expressions and variables The memory allocated for an expression inside the function chAddWat chExpr is freed in function chRemoveWatchExpr and chRemoveWatchExprs The command remove expr calls func tion chRemoveWatchExpr to remove the expression expr from the watched list For the command remove function chRemoveWatchExprs is called to remove all expressions in the watched list Function chRemoveWatchExprs is also called by chCleanDebug at the end of the program ex ecution Function chProcessWatchExprs is called in function chCallback in Program 7 17 when the progarm is stopped by step or next command or in a breakpoint to display each expressi
106. m a 20 return a int main int n 2 m 3 array int a 2 3 array int b 2 3 a funcl b func2 n m printf a in Ch n d a printf b in Ch n d b return 0 Program 6 6 Ch program with functions that return computational array returnarray ch 124 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 2 CALLING CH FUNCTIONS WITH BUILT IN STRING TYPE STRING T KK kk kk kk kk kk kk kk KCkCkCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCkCk kk k k k Ck k kk k kok kok kok kok I k File Name returnarray c CKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCKCKCkCKCkCKCkCKCk Ck k ck k ck k ck k ck k kk kk kk ok ko ke ke ex include lt stdio h gt include lt embedch h gt int main ChInterp t interp int n 2 m 3 int a 2 3 b 2 3 int status retval char argvv returnarray ch NULL ChVaList_t arglist Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program returnarray ch failed n Ch_CallFuncByName interp funcl a or Ch_CallFuncByNameVar interp funcl a NULL printf a 1 1 in C Sd n a 1 1 arglist Ch VarArgsCreate interp Ch_VarArgsAddArg interp amp arglist CH_INTTYPE n Ch_VarArgsAddArg interp amp arglist CH_INTTYPE m Ch CallFuncByNameVar interp func2 b arglist Ch VarArgsDelete interp arglist printf b 1
107. number of symbols is 5 symbol index 0 name _ func address 47ff8 symbol index 1 name i address 55fe0 symbol index 2 name x address 56010 symbol index 3 name c address 50b10 symbol index 4 name c2 address 512e0 i 10 The stack level is 2 The current line number is 0 The current file name is cplus cpp The total number of symbols is 4 symbol index 0 name i address 53750 symbol index 1 name g address 44f90 symbol index 2 name a address 471e8 symbol index 3 name main address 4c0c8 i 40 Figure 7 3 Output from executing prog2 cplus cpp 142 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK int i 40 double g 100 void nestedfunc int i double g 300 rnt void funcl int i double g 200 int y printf funcl is called Win if i int i 20 z i 30 printf argument i in funcl is true n funcl i int main int i 10 x nestedfunc i return 0 Program 7 5 Ch program with a nested function nestedfunc ch 143 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK funcl is called argument i in funcl is true event MASKEND is true The stack level is O0 The current line number is 17 Function funcl starts at line 8 Function funcl ends at line 18 The function name is funcl The current file name is nestedfunc ch The total number of symbols
108. on page 278 More information about Ch GlobalSymbolAddrByName can be found in Ch SDK User s Guide Other functions with the prefix Ch GlobalSymbol can be found in the reference described Appendix A 2 Accessing the variable by its address C program accesschvar c in Program is an example of accessing global variables in a Ch program After function Ch RunScript is called only global variables in the Ch space can be accessed The Ch program accesschvar ch shown in Program 2 2 has two global variables ii with type int and dd with type double The data type of a variable in the Ch space can be obtained by the API Ch DataType which returns a value for a valid Ch type defined in the header file ch h Accordingly two variables pii with pointer to int and pdd with pointer to double are declared in accesschvar c for keeping addresses of those two Ch variables The size of a variable or expression can be obtained by the function Ch DataSize After the embedded Ch is initialized Program 2 2Jis loaded and executed Statements below pii Ch SymbolAddrByName interp ii pad Ch SymbolAddrByName interp dd obtain addresses of variables ii and dd in Ch space Then the statement 29 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 1 ACCESSING VARIABLES BY ADDRESSES KKK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kok kok k k Ck kk kk kok ck k kok I OK k File Name accesschvar c Accessing global variables in the Ch pro
109. only returns the fixed number of arguments In this case function Ch IsFuncVarAr rg returns 1 Example See Ch DataType See Also Ch DataType Ch ArrayDim Ch ArrayExtent Ch ArrayType Ch FuncType Ch FuncArgNum Ch UserDefinedName Ch UserDefinedSize 272 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgUserDefinedName Ch FuncArgUserDefinedName Synopsis include lt ch h gt char Ch FuncArgUserDefinedName ChInterp t interp const char funcname int argnum Purpose Obtain the tag name of a user defined data type in terms of struct class or union and its pointer type for an argument of function in the Ch space Return Value If the argument of the function in the Ch space is a user defined data type this function returns the address of the tag name of the user defined data type Otherwise it returns NULL Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description This function returns the address for the tag name of struct class union or its pointer type for an argument of function in the Ch space Function Ch FuncArgUserDefinedSize can be used to obtain the size of the user defined type Example See Ch_FuncArgDataType See Also Ch FuncArgDataType Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgArrayType O Ch FuncArgIsFunc C
110. printValue in Program 7 9 If a variable is not user defined type the arguments dat at ype for the data type and addr for the address of the variable will be able to obtain the value of the variable Each case in the switch statement in the function printValue prints out the value based on its data type For example the statement printf d int addr prints out a variable of type int Many other data types such as short listed in the header file CHHOME extern include ch hcan be added to cases for the switch statement Function printValue returns the size of the processed data type This returned size of the data type for a variable can be used to print out values for an entire array For a variable of user defined type it calls function printUserDefined in Program 7 9 to print out each member of the user defined type which will be described later If a variable is array type the statements dim Ch ArrayDim interp name n Ch ArrayExtent interp name 0 obtain the dimension of the array and the extent for its first dimension For a two dimensional array the extent for the second dimension is obtained by the statement m Ch ArrayExtent interp name 1 The values for each element of the two dimensional array are processed by two nested for loops The returned value of the function printValue for the size of an element of the array is used to obtain the address of the next element of the array by the st
111. printf a2 n d a2 printf b2 n d b2 d n S23 Sd n sp gt i d n cui d n cp gt i d n usa Sd n up gt i Sd n sa gt i printf s i printf sp gt 1 printf c i printf cp gt 1 printf u i printf up gt i printf sa gt 1 Satt l oe oe oe printf sa i dMn sa gt i printf fp Sp n fp printf fp2 Sp n fp2 fp2 fprintf stream callback called n return 0 Output i printed from setvar c 10 dp 10 000000 chp string in main a 1 1 5 b 1 2 a2 d 3 45 6 b2 123 s i 10 sp gt i 10 Gi 10 cp gt i 10 u i 10 up gt i 10 sa gt i 10 sa gt i 1 fp 0 fp2 49a40 func called callback called Example 2 Programs 2 5 2 6 7 11 See Also Ch_AppendParseScriptFile Ch_AppendParseScript Ch_AppendRunScript and Ch_VarArgsAddArg in Ch SDK User s Guide 298 Ch_SetVar Ch AppendRunScriptFile Chapter A APIs for Embedding Ch lt embedch h gt Ch StackLevel Ch StackLevel Synopsis include lt ch h gt int Ch_StackLevel ChInterp_t interp int clevel int hlevel Purpose Obtain the current and highest possible stack levels Return Value This function returns CH OK on success and CH ERROR on failure The function returns the highest possible level of the stack The stack levels range from 0 to a positive integer number Parameters interp A Ch
112. program that invokes embedded Ch programs In this case these functions shall be exported so that Ch programs can callback these binary functions The example below demonstrates the mechanism for interface between Ch and C for such callback functions using a dynamically loaded library Because this example uses the same functions in section we only list programs that are different from the ones in the example in section 5 1 In this example function files unc1 chf and func2 chf interface file sample chdl c Ch script program ch application program prog c are the same as before Program 5 23 for func c contains two functions funcl and func2 which are exported To export a function for callback from Ch scripts the function shall be defined and prototyped with type qualifier EXPORTCH as shown in Program 5 23 The corresponding header file sample h applicable in both C and Ch spaces is shown in Program 5 24 File func c is compiled and linked with Program 5 1 for program prog c to form an executable binary program so that function unc1 can be called inside program prog c Unlike the example in section 5 T the handler Chsample handle for the dynamically loaded module is obtained by the programming statement Chsample handle dlopen libsample dl RTLD LAZY which loads the module explicitly The statement extern void Chsample handle dlopen libsample dl RTLD LAZY loads the dynamically load
113. program with debugging or run the program without debugging The user can execute the program line by line either by command step or next The command step will step into a function whereas or the command next will step over the function to the next line During the debugging the command cont can be invoked to continue the execution of the program till it hits a breakpoint or the program ends The user can change the function stack during debugging It can go up to its calling function or move down to the called function so that the variables within its 166 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER scope can be accessed in the debugger The function or program names in all stacks are displayed by the command stack Names and their corresponding values of variables in the current scope are displayed by the command locals Names and their corresponding values of variables in all stacks can be displayed by the command variables The command wat ch adds an expression including a single variable into the list of watched expressions when the program is stopped at a breakpoint or stepped into next statement An expression can be removed from the list of the watched expressions by the remove expr command All expressions in the watched list can be removed by the remove command Before the program execution or during the debugging of an executed program new breakpoints can be added to stop the program execution or cle
114. s not found type help n line break return 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 192 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER Get the action item from user menu return a proper enum in enum Action Successful return ACTION UNDEFINED failed m action t getActionMenu char str action t act const char prompt debug gt printf s prompt fgets str MAX PROMPT STR stdin str strlen str 1 0 if strnemp str load strlen load act ACTION LOAD else if strncomp str start strlen start act ACTION START else if strncomp str run strlen run act ACTION RUN else if strcmp str step act ACTION STEPINTO else if strcmp str next act ACTION STEPOVER else if strcmp str cont act ACTION CONTINUE else if strcmp str up act ACTIO UPSTACK else if strcmp str down act ACTION DOWNSTACK else if strcmp str stack act ACTION STACK else if strcmp str locals act ACTION LOCALS else if strcmp str variables act ACTION VARIABLES else if strnemp str watch strlen watch act ACTION_ADDWATCHEXPR else if strcmp str remove act ACTION REMOVEWATCHEXPRS else if
115. strncomp str remove strlen remove act ACTION REMOVEWATCHEXPR else if strncomp str stopat strlen stopat act ACTION SETBREAKPOINTLINE Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 193 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER else if strncmp str stopin strlen stopin act ACTION SETBREAKPOINTFUNC else if strncomp str stopvar strlen stopvar act ACTION SETBREAKPOINTVAR else if strncomp str clearline strlen clearline act ACTION CLEARBREAKPOINTLINE else if strncomp str clearfunc strlen clearfunc act ACTION CLEARBREAKPOINTFUNC else if strncmp str clearvar strlen clearvar act ACTION CLEARBREAKPOINTVAR else if strcmp str clear act ACTION CLEARBREAKPOINTS else if strcmp str help act ACTION HELP else if strncomp str assign strlen assign act ACTION ASSIGN else if strncmp str call strlen call act ACTION CALL else if strncomp str print strlen print act ACTION PRINT else if interp NULL amp amp Ch ExprParse interp str CH OK act ACTION EXPR else if strcmp str abort act ACTION ABORT else act ACTION UN
116. t ap ch int n void memhandle int retval Ch VaStart interp ap varg n Ch VaArg interp ap int ap ch Ch VaArg interp ap ChVaList t get variable argument list in C ap c Ch VaVarArgsCreate interp ap ch amp memhandle retval vfunc5 n ap c delete the temp memory Ch VaVarArgsDelete interp memhandle Ch VaEnd interp ap return retval main ChInterp t interp int status char str 1024 x initialize embedded Ch h Initialize amp interp NULL Q Ch DeclareVar interp int ch i 10 ch p 239 Ch DeclareFunc Chapter A APIs for Embedding Ch lt embedch h gt Ch DeclareFunc s2 sp Ch DeclareVar interp struct tag int i p s 10 NULL create new data type tagptr t in the Ch space Ch DeclareVar interp struct tag tagptr t Ch DeclareTypedef interp tagptr t Ch DeclareFunc interp void funcl double x ChFuncdl t funcl chdl Ch ExprEval interp funcl ch i or Ch DeclareFunc interp struct tag func3 struct tag sp ChFuncdl t func3 chdl Ch ExprEval interp sp func3 amp s s Ch ExprEval interp printf sp gt i d n sp gt i SPECIAL CASE for function returning a struct ChFuncdl t funo4 chdl Ch ExprEval interp s2 func4 10 s Ch DeclareFunc interp struct tag func4 i
117. text that appears on the screen of your display or external monitor It can be a system message for example or it can be a text that you are instructed to type as part of a command referred to as a command line Screen text is presented in the typewriter like typeface Example The following message appears on your screen usage rm fiRr file ls aAbcCdfFgilLmnopqrRstuxl1 file Function prototype consists of return type function name and arguments with data type and param eters Keywords of the Ch language typedefed names and function names are presented in boldface Parameters of the function arguments are presented in italic The brackets indicate items that are optional Example double derivative double func double double x double err double 7 Source code of programs is presented in the typewriter like typeface Example The program hello ch with code int main printf Hello world in will produce the output Hello world on the screen Variables are symbols for which you substitute a value They are presented in italics Example module n where n represents the memory module number System Variables and System Filenames are presented in boldface Example startup file home username chre or chre in directory home username in Unix and C gt _chre or _chre in directory C gt in Windows Identifiers declared in a program are presented in typewriter like typeface when they a
118. that the program will stop at the function again when it is called next time For a breakpoint with a controlling variable it will first find the address of the variable by the statement varptr void Ch SymbolAddrByName interp bp gt u var varname If the variable is accessible within the current program scope the value of the variable will be saved in bp gt u var varvalue when it is encountered the first time Otherwise it will be checked against the 172 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER previously saved value If the current value is different from the saved one the new value will be saved and the breakpoint is hit Function chTestBreakPointCondition uses the APIs Ch ExprParse and Ch ExprCalc to evaluate the conditional expression in bp condexpr The value of the expression is saved in bp condvalue The memory for bp condvalue is allocated dynamically based on the data type and its size of the expression when the conditional expression is processed for the first time If the mem ber bp condtrue is true we only test if the conditional value is true or not Otherwise we test if the conditional value has changed If the breakpoint should be hit the function returns 0 Otherwise it returns 1 The allocated memory for breakpoints are freed by the function chClearBreakPoints called in function chCleanDebug at the end of program execution 7 4 1 A Sample Debugger
119. threads 15 nmake 6 pragma putenv 6 remove variables 242 remvar 242 return array 121 scope B16 scope resolution operator 816 STDERR FILENO STDIN FILENO STDOUT FILENO stradd 6 string t 6 system variable thread typographical conventions va count 61 va elementtypeO 61 va_end 61 va start 61 variable 305 variable length argument T 16 variable length array 116 variable number of arguments 60 Visual NET 7 Visual C 6 VLA see variable length argument Windows 6 325
120. to be evaluated in the Ch space result the result of the evaluated expression Description The function Ch_ExprValue executes an expression in the Ch space It can be used to evaluate an ex pression or call a function in Ch space The expression expr may invoke functions located in function files specified by the function file path fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is an operand of an expression The address for the result of the evaluated expression is passed through a generic pointer in the third argument of the function The function returns a value node for the result of the evaluated expression This value node is used to keep track of the temporary memory of the result of the evaluated expression This value node should deleted by the function Ch DeleteExprValue later to free the allocated memory See API Ch SymbolAddrByName how the returned address of an expression is handled for some special cases Unlike function Ch ExprCalc the user does not need to declare a variable of a proper data type to obtain the value of an expression Example See function chPrint in Program See Also Ch SymbolAddrByName Ch_DataType Ch CallFuncByAddr Ch_CallFuncByName Ch ExprCalc Ch ExprParse Ch ExprEval Ch DeleteExprValue 255 Chapter A APIs for Embeddi
121. valid index or the function call is successful Example See Ch DataType See Program 2 9 See Also Ch DataType Ch UserDefinedInfo Ch UserDefinedMemInfoByIndex Ch UserDefinedTag 314 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedTag Ch UserDefinedTag Synopsis include lt ch h gt ChUserDefinedTag_t Ch_UserDefinedTag ChInterp_t interp const char name Purpose Obtain the tag for a variable of user defined structure class or union type in the Ch space Return Value If the argument of the variable in the Ch space is a user defined structure class or union type this function returns the tag for the user defined data type Otherwise it it returns NULL For a variable for a tag name array or pointer type in the Ch space the function also returns its tag Parameters interp A Ch interpreter name Name or tag name for a variable or expression of user defined type in the Ch space Description This function Ch UserDefinedTag returns the tag of a user defined structure class or union or its pointer type for a variable in the Ch space Function Ch_UserDefinedInfo can be used to obtain the tag name size number of members of the user defined type Example See Ch_DataType Programs 2 9 and See Also Ch DataType Ch UserDefinedInfo Ch UserDefinedMemInfoByIndex Ch UserDefinedMemInfoByName 315 Chapter A APIs for Embedding Ch lt embedch h gt Ch VarType
122. varname hClearBreakPoints debugInfo t dgptr hAssign debugInfo t dgptr char expr hCall debugInfo t dgptr char expr hPrint debugInfo t dgptr char expr hExpr debugInfo t dgptr char expr hAbort debugInfo t dgptr on below for Menu processing int hProcessActionMenu debugInfo t dgptr cplusplus DEBUG H Program 7 13 The header file with defined data structures and function prototypes for the debug program debug h Contd File Name prog6 c include debug h int main chHelp wh ile 1 debugInfo t debug chInitDebug amp debug chProcessActionMenu amp debug chEndDebug amp debug return 0 Program 7 14 The debug program prog6 c 177 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM File Name menu c include lt stdio h gt include lt string h gt include debug h int chHe prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin prin 1 Cb cp cb ich ET CB PCT CR cP Gk Sah Gk ick CT chy Gt ah seb ek ich check eh GP ich ck ich et TR load filename start args run args step stopat filename cond stopin funcname cond stopvar varname cond Clearline filename 4 clearfunc funcname clearvar varname
123. void level 10 1 this_part_of_the_function_level0_is_being_executed int main 301 Chapter A APIs for Embedding Ch lt embedch h gt Ch StackName level2 return 0 As an example a callback function is invoked when the code inside function 1evel0 in the above code is executed The function name levell of the calling function 1evell and program name prog c can be obtained as follows char funcname progname int level 1 isfunc funcname ChStackName interp level amp isfunc isfunc becomes 1 level 3 progname ChStackName interp level amp isfunc isfunc becomes 0 Example See function chStack in Program See Also Ch ChangeStack Ch StackLevel 302 Chapter A APIs for Embedding Ch lt embedch h gt Ch SymbolAddrByIndex Ch SymbolAddrByIndex Synopsis include lt ch h gt void Ch SymbolAddrByIndex ChInterp t interp int num Purpose Get the address of a variable in the symbol table for variables and arguments of a function or in the symbol table for global variables in a Ch program Return Value If the control flow of the Ch program is inside a function the function returns the address of a variable in a function if the index number is out of the valid range for its symbol table the function returns NULL If the control flow of the Ch program is outside a function the function returns the address of a global variable if t
124. with user defined options embedch2 c include lt stdio h gt int fun int i printf i in fun return 0 d n i int main int argc char argv int i 0 printf argc d n argc while argv i NULL printf argv d s n i argv i lider fun 5 return 0 Program 1 6 An embedded Ch program embedch2 ch 12 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 4 INCREMENTAL EXECUTION BY APPENDING CODE TO AN EMBEDDED CH PROGRAM After Ch RunScript is called Ch CallFuncByName can be used to invoke different functions inside the embedded Ch program separately as follows Ju a AN int status retval status Ch RunScript interp argvv Ch CallFuncByName interp funcl amp retval Ch CallFuncByName interp func2 NULL uuu 8 1 4 Incremental Execution by Appending Code to an Embedded Ch Pro gram For some applications it is desirable to append a piece of code to an embedded program after it has been executed Embedded Ch provides this capability through functions Ch AppendRunScript and Ch AppendParseScript These functions append a piece of code to an embedded Ch program exe cuted by functions Ch RunScript or Ch ExecScript Like other APIs the first arguments of func tions Ch AppendRunScript and Ch AppendParseScript are Ch interpreter The second argument of these two functions contains the code to be appended to the existing embedded Ch progr
125. 1 in C Sd n b 1 1 Ch End interp Program 6 7 A C program calling Ch functions that return computational array returnarray c a in Ch 10 10 10 10 10 10 b in Ch 20 20 20 20 20 20 a 1 1 in C b 1 1 in C 10 20 Figure 6 3 Output from executing returnarray c 6 2 Calling Ch Functions with Built in String Type st ring_t String is a built in data type with type specifier st ring t in Ch In this section we will describe how to call Ch functions with string type string t The syntax for calling Ch functions with string type string t is the same as that for calling regular Ch functions except for the following two constraints 125 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 2 CALLING CH FUNCTIONS WITH BUILT IN STRING TYPE STRING T string t func string t s string t s2 printf s in func s n s S2 stradd s ABCD return s2 Program 6 8 Ch program with a function with an argument and return type of string type string t callstring ch 1 For a function with argument of string type st ring_t a pointer to char shall be passed to it from C 2 If the return type of a function is string type st ring t the return value can be assigned to a variable of pointer to char The allocated memory from the function assigned to the pointer to char needs to be freed when it is no longer needed Functions func in Program 6 8 has an argument and return type of st
126. 1 is passed to m will be returned Example See Ch FuncArgDataType See Also Ch FuncArgDataType Ch FuncArgArrayDim Ch FuncArgArrayTypeO Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch_FuncArgFuncArgNum Ch_FuncArgUserDefinedName Ch FuncArgUserDefinedSize 258 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgArrayNum Ch FuncArgArrayNum Synopsis include lt ch h gt int Ch FuncArgArrayNum ChInterp t interp const char funcname int argnum Purpose Obtain the number of elements for an argument of array type for a function of global variable in the Ch space Return Value If the argument of the function for a global variable in the Ch space is of array type This function returns the number of elements of the array Otherwise it returns 0 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description This function returns the number of elements of array type for a function of global variable in the Ch space The dimension of the array can be obtained by function Ch_FuncArgArrayDim Example See Ch_FuncArgDataType See Also Ch_FuncArgDataType Ch FuncArgArrayDim Ch_FuncArgArrayExtent Ch_FuncArgArrayType Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch_FuncArgFuncArgNum Ch FuncArgUserDefinedName Ch FuncArgUserDefinedSize 259 Ch
127. 10 20 0 40 0 Ch End interp Program 3 12 Calling Ch functions taking variable number of arguments by expression evaluations callchvna2 c calling func2 from C space vacount 2 val i 10 val d 20 000000 vacount 3 val i 10 val d 20 000000 val d 40 000000 calling func3 from C space vacount 2 val i 10 val d 20 000000 vacount 3 val i 10 val d 20 000000 val d 40 000000 Figure 3 6 Output of executable compiled from Program 65 Chapter 4 Accessing C C Variables from Ch Space In Chapter 2 we discussed how to access variables in Ch space from C space with embedded Ch Because both C and Ch use the same memory layout internally accessing variables and data in C space from Ch space is much simpler than any other scripting languages Accessing global variables in C space in static or dynamically loaded library declared in a header file from Ch space has been described in Chapter 4 in Ch SDK User s Guide The methods are readily applicable to embedded Ch programs However with Embedded Ch interface between C and Ch is much simpler This chapter describes the methods to access variables and share data in C space from Ch space in an embedded Ch program Section 2 2 in Chapter 2 described how to use the API Ch_SetVar to assign values in C space to variables in Ch space Section 4 IJillustrates how to share the data in C space from Ch space using a func
128. ByName interp chfun NULL printf in embedch3 c amp c var p c var d n amp c var c var Ch End interp return 0 Program 4 10 Example of accessing variables in C Space from Ch space embedch3 c 75 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 3 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE WITH DL FILE include lt dlfcn h gt void chfun void handle fptr int ch_var ch_varptr handle dlopen libembedch3 dl RTLD LAZY if handle NULL printf Error s dlopen s n _ func dlerror return fptr dlsym handle embedch3 chdl if fptr NULL printf Error s dlsym s n __func__ dlerror return dlrunfun fptr sch var NULL sch varptr if dlclose handle 0 printf Error s dlclose s n __func__ dlerror return printf in the Ch function ch var d n ch var ch_varptr 10 Program 4 11 Example of accessing variables in C Space from Ch space embedch3 ch include lt ch h gt IMPORTCH int c_var EXPORTCH int embedch3_chdl void varg ChInterp t interp ChVaList t ap int c varptr2 Ch VaStart interp ap varg c varptr2 Ch VaArg interp ap int c varptr2 amp c var printf in embedch3 chdl c amp c var p c var d n amp C var C var Ch VaEnd interp ap return c var Program 4 12 Example of accessing variab
129. CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES File Name stack c include lt stdio h gt include lt string h gt include lt embedch h gt include debug h stactic functions within the file static int printUserDefined ChInterp_t interp ChType_t datatype void addr ChUserDefinedTag_t udtag int nestlevel static int printValue ChInterp_t interp ChType_t datatype void addr ChUserDefinedTag_t udtag int nestlevel int displayAll 1 if 1 display definition of member functions etc if 0 do not display definition of member functions etc void chPrintSymbolValuesInStack ChInterp t interp int level int index totalnum isfunc char stackname name classname void addr tackname Ch StackName interp level amp isfunc amp classname totalnum Ch SymbolTotalNum interp if isfunc 1 printf Function s at stack level d has d symbols n stackname level totalnum else if isfunc 2 printf Member Function s s at stack level d has d symbols n o classname stackname level totalnum else printf Program s at stack level d has d symbols n stackname level totalnum for index 0 index lt totalnum index name Ch SymbolNameByIndex interp index addr Ch SymbolAddrByIndex interp index chPrintSymbolValue interp name addr printf n separate the stack level in the output void c
130. CFLAG o thread exe threads exe threads c CC threads c INC S LFLAG CFLAG o threads ex lpthread append exe append c CC append c INC S LFLAG CFLAG o append exe clean rm f o obj exe dl lib exp Program 1 2 Makefile in Unix for all examples in this chapter CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 2 BUILDING EXECUTABLES IN WINDOWS l f r C Space embedch1 exe Ch Space ae mibedchieh main l Ch Initialize I o Ch RunScript o o K o M A o Ch End M Figure 1 1 Core APIs of embedded Ch status Ch RunScript interp argvv runs the Ch program indicated in argument argvv 0 in the Ch interpreter initialized by function Ch Initialize The return value status is CH OK on successful execution CH ERROR on failure These two macros for return status are defined in header file ch h Before the program exits function Ch End needs to be called to end the embedded Ch initialized by function Ch Initialize Function calls Ch Initialize at the beginning and Ch End at the end are required for embedding Ch into C C applications Function Ch RunScript can be replaced by other APIs in the Embedded Ch Finally The libraries 1ibchsdk aand libembedch a for Unix or chsdk liband embedch 1lib for Windows located in the directory CHHOME extern 1lib
131. COUNT CH MASKEND CH MASKLINE CH MASKNONE CH MASKRET CH NOTFUNCTYPE CH NOTVARTYPE 323 CH OK Ch_ParseScript 9 CH REGULARCH Ch Reopen Ch RunScript Ch RunScriptM CH_SAFECH 11 203 286 Ch SetGlobalUserData 19 202 293 Ch SetVar Ch StackLevel Ch StackName Ch SymbolAddrBylIndex Ch_SymbolAddrByName Ch SymbolIndex Ch SymbolIndexByName Ch SymbolNameBylIndex Ch SymbolTotalNum Q 31 CH_UNDEFINETYPE Ch_UserDefinedInfo Ch UserDefinedMemlnfoBylIndex 33 202 311 Ch UserDefinedMemlnfoByName B3 314 Ch UserDefinedName Ch UserDefinedSize Ch UserDefinedTag Ch VaArg Ch VaEnd Ch VarArgsAddArg Ch VarArgsAddArgExpr Ch VarArgsAddArg Var Ch VarArgsCreate Ch VarArgsDelete Ch_VarType Ch_VaStart Ch_VaVarArgsCreate 237 Ch_VaVarArgsDelete 237 Ch_Version ChBlock t classname 130 count event funcname isconstructor isdestructor Chapter B APIs for Embedding Ch lt embedch h gt level linecurrent linefuncbegin linefuncend 130 212 source 130 ChCallback t ChFile_t 22 199 290 ChFuncdl t 199 236 ChFuncType t ChInfo t ChInterp t ChMemlnfo t ChOptions t chhome chmtdir 320 chrc 320 chrcname shelltype ChPionter t ChPointer t ChRun ScriptM chsdk lib chsdk bc lib 9 chsdk mdd lib 6 chsdk mt lib 6 chsdk mtd
132. Ch DataType Ch ExprEval Ch End 247 Chapter A APls for Embedding Ch lt embedch h gt Ch ExprCalc Ch ExprCalc Synopsis include lt ch h gt int Ch_ExprCalc ChInterp_t interp const char expr ChType_t datatype void result Purpose Calculate an expression in Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter expr Expression to be executed in Ch space datatype The data type of the result result Pointer to the memory for holding the result of the expression Description The function Ch ExprCalc executes an expression in Ch space It can be used to evaluate an expression or call a function in Ch space The expression expr may invoke functions located in function files specified by the function file path _fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is an operand of an expression If the actual argument for parameter result of data type datatype is not a NULL pointer the result will be assigned to the memory pointed by result based on the data type ChType t of the argument datatype listed in the header file ch h described in Ch SDK User s Guide The user has to make sure a memory of the proper size is allocated for result If the passed argument of result is NULL no value will be assigned
133. Ch ParseScript Ch ExecScript 216 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendParseScriptFile Ch AppendParseScriptFile Synopsis finclude lt embedch h gt int Ch AppendParseScriptFile ChInterp t interp const char filename Purpose Append a fragment of the Ch code in a file to an existing Ch program inside the interpreter executed by functions Ch RunScript or Ch ParseScript and Ch ExecScript The Ch code will be parsed without execution Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter filename File name that contains the Ch code to be appended to the existing Ch program inside the inter preter Description When a program is run in Ch it first will be parsed then executed The process is similar to compile and execution using a C compiler Like Ch RunScript But Ch AppendParseScriptFile parses the code only The code is not executed Functions Ch ExprEval Ch CallFuncByAddr or Ch CallFuncByName shall be called to execute the code This implies that only appended functions can be executed The restrictions of Ch AppendParseScript do not apply to Ch AppendParseScriptFile See Also Ch AppendParseScript Ch AppendRunScriptO Ch AppendRunScriptFile Ch RunScript Ch ParseScript Ch ExecScript 217 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendRunScript Ch AppendRunScript Synop
134. ChInterp_t interp const char name Purpose Determine if a variable in the Ch space is a function type Return Value If the argument is not a variable of function type this function returns CH NOTFUNCTYPE with the value of 0 If the argument is a variable of function type this function returns a non zero value of ChFuncType t The data type ChFuncType t defined inside the header file embedch h has the following values Value Description CH NOTFUNCTYPE not function CH FUNCTYPE regular function CH FUNCPROTOTYPE function prototype without function definition CH FUNCPTRTYPE pointer to function CH FUNCMEMBERTYPE function of a class CH FUNCCONSTYPE constructor of a class CH FUNCDESTTYPE destructor of a class Parameters interp A Ch interpreter name Name of the variable in the Ch space Description The function Ch FuncType determines if the argument is a variable of function type The function type includes function with definition function prototype without function definition pointer to function mem ber function of a class contructor or destructor of a class Example See Ch DataType Program 7 8 See Also Ch DataType Ch ArrayDim Ch ArrayExtent Ch ArrayType Ch_IsFuncVarArg Ch FuncArgNum Ch UserDefinedTas Ch UserDefinedInfo Ch VarTypeO 275 Chapter A APIs for Embedding Ch lt embedch h gt Ch GetGlobalUserData Ch GetGlobalUserData Synopsis include lt embedch h gt ChP
135. D thread result N int i arg N if Ch Initialize amp interp NULL CH ERROR printf Error Ch Initialize failed in main n Create thr new threads for i 0 i lt N i arg i i 1 if i 0 a thread i CreateThread NULL 0 thread functionl PVOID interp 0 ga threadId i else if i 1 a thread i CreateThread NULL 0 thread_function2 PVOID interp 0 ga threadId i if a_thread i NULL perror Thread creation failed exit EXIT_FAILURE printf Waiting for threads to finish n for i 0 i lt N i if WaitForSingleObject a thread i INFINITE WAIT OBJECT 0 perror Thread join failed exit EXIT FAILURE Ch End interp Retrieve the code returned by the thread for i 0 i lt N i GetExitCodeThread a_thread i amp thread result i printf Thread joined it returned d n thread result i exit EXIT_SUCCESS See Also Ch Initialize Ch_End 209 Chapter A APIs for Embedding Ch lt embedch h gt Ch AddCallback Ch AddCallback Synopsis include lt ch h gt int Ch_AddCallback ChInterp_t interp int event ChCallback t callback ChPointer t clientdata int count Purpose Add events and callback function Return Value If the function is successful it returns CH OK Otherwise it returns CH ERROR Parameters
136. DEBUGGER run mode typedef enum RUNMODE_UNDEFINED from commands in a GUI RUNMODE_START command start with debugging RUNMODE_RUN command run without debugging RUNMODE_STEPINTO command step into RUNMODE_STEPOVER command next to step over RUNMODE_CONTINUE command cont to continue runmode t The structure for a breakpoint typedef struct BreakPoint action_t btype the breakpoint type One of values of ACTION_SETBREAKPOINTLINE ACTION_SETBREAKPOINTFUNC ACTION SETBREAKPOINTVAR union struct char filename file name int linenum the line number for the breakpoint file struct char funcname function name int called 1 if the func is called reset to 0 when function is returned func struct char varname variable name void varvalue value of variable Jvar Ju If the conditional expression for a breakpoint is NULL or the conditional value is true or has changed the breakpoint will be handled otherwise skip it char condexpr condition expression void condvalue condition value int condtrue 1 for the conditional value is true to trigger the breakpoint 0 for the condtional value has changed to trigger the breakpoint struct BreakPoint next next breakpoint in the linked list breakPoint t T
137. DEFINED return act void printErrorNoFile void use MessageBox for GUI in Windows printf Error no program to be debugged Wn this function creates char argvv prog ch optl opt2 opt3 NULL from command start optl opt2 opt3 dynamically It also handles argument within double quotation marks start optl opt2 with space opt3 int getArgvFromStr char argv char args int i 1 char str token buf MAX PROMPT STR str args while str NULL amp amp str skip the blank space strt t Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 194 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER if str NULL str N0 return 0 bt estE o AW 4 token strtok str t tab and space as delimiters if token K start strl str2 str3 strcpy buf token 1 kickout and get strl token strtok NULL if token NULL not start str or start str strcat buf Nt is treated as space strcat buf token get strl str2 if token strlen token 1 NO se not finished token strlen token 1 amp amp not space token strlen token 1 Nt not tab token strtok NULL t strcat buf token get strl str2str3 token buf argv it
138. E 2 5 REMOVING AND REDECLARING VARIABLES var in removevar ch 10 var 10 var 100 000000 var 1 000000 2 000000 3 000000 4 000000 5 000000 6 000000 Figure 2 8 Output of the executable program from removevar c printed out API Ch SymbolAddrByNamew returns the address of a global variable by name in the Ch Space If the name is not a global variable it returns NULL In Program 2 13 it is used to check whether the variable var exists in program removevar ch If it exists the variable is removed by the function call Ch AppendParseScript interp pragma remvar var Then the name var is redeclared as a floating point variable of float type and initialized with the value 100 0 After it is printed out the variable is again removed It is then redeclared as a computational array of double type and initialized with values for each elment of the array by the following statements Ch AppendParseScript interp include lt array h gt Ch AppendParseScript interp array double var 2 3 1 2 3 4 5 6 The entire array is then printed out The output from in Program is shown in Figure 46 Chapter 3 Calling Ch Functions from C Space In the previous chapters we introduced methods of accessing variables and evaluating expressions in Ch programs from C C programs with embedded Ch Furthermore users can call Ch functions in C C programs C space There are four methods to call Ch functions from the C space
139. E DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES 7 3 Manipulating Local and Global Variables Except for APIs starting with the prefix Ch GlobalSymbol for handling global variables most other APIs in Embedded Ch can be used to manipulate both local and global variables Given a name an API can search the variable in symbol tables within its scope All information about a variable or expression can be obtained by a proper API The value for a variable can also be changed Details of using APIs for manipulating variables and functions in the Ch space have been presented Chapters Bland B In this section two sample application programs are presented to illustrate how to use various APIs in a callback function to manipulate variables in a Ch script The first example prints out values for each variable in symbol tables in a script The second example shows how to set a value for a variable and evaluate an expression using variables Based on the ideas presented in these two examples comprehensive debuggers profilers IDEs and other tools can be developed 7 3 1 Obtaining Values of Variables The callback function in the presented program in this section can print out the value for each variable within the scope of the executed program at its active line of the code It will print out the values for the local variables members of the class for a member function and arguments of the function first Then it
140. Eval The argument of command print is an expression The expression and its value obtained by the API Ch_ExprValue are printed out by function chPrintSymbolValue in Program The expression of command print is processed inside the function chPrint Functions Ch ExprValue and Ch DeleteExpr Value are used to obtain the value of expression of different data types The calculated result of different data type is then displayed One can also just type expression its value will be printed out A valid expression is processed by function chExpr Function chExpr is similar to function function chPrint except that it will not display an error message when the expression is of a function with return type of void As an example commands debug assign i 2 10 debug call func debug print i 20 debug 2 i 40 debug assign the variable i with the value of 10 call function func and print out the value of the expression 2 i when the variable i is valid in its current scope The last function chAbort in Program 7 16 corresponds to the command abort The function chAbort is similar to function chEndDebug in Program 7 17 When the interpreter is execut ing the callback function which invokes the function chAbort the interpreter is aborted by the API Ch Abort for the command abort The allocated memory for the debugger is then freed by the function chCleanDebug The callback function chCallback in
141. Ex len strlen proghome strlen embedch 1 option chhome char malloc len strcpy option chhome proghome strcat option chhome embedch Ch_Initialize amp interp amp option JR pbes ops Pa Ch End interp free option chhome Program 1 14 A sample code for distribution of Embedded Ch without regular Ch 26 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 7 DISTRIBUTING EMBEDDED CH IN YOUR APPLICATIONS Ch Initialize interp amp option as shown in Program The startup file option chhome config chrc or C myApplication embedch config chroc will be executed in this initialization function Embedded Ch searches for dynamically loaded libraries such as chmt 1 d1 and chmt2 d1 in the directory option chhome extern lib Multiple Ch interpreters can be embedded in an application Each instance of Ch interpreter is dynam ically loaded The dynamically loaded library chmt1 d1 is for the first instance of Ch interpreter chmt2 dl for the second one and chmtn d1 for the nth one Although these dynamically loaded libraries are exactly the same different copies are needed if your application has multi threads For a multi thread application which needs n instances of Ch interpreter copy CHHOME toolkit embedch extern lib chmtl dl to chmt2 dl chmt3 dl and chmtn dlinoption chhome extern lib The maximum number of simultaneous instances of Ch interpreter is 500 4 Like regular Ch shell a
142. Funcdl t ChOptions t ChPointer t ChUserDefinedTag t ChUserDefinedInfo t ChMemlInfo t ChValueNode t ChVarType t Description A structure containing the information for a function block used by Ch AddCallback and Ch ChangeStack The data type for callback function used by Ch AddCallback File descriptor I O redirection in Ch Reopen Ch Flush and Ch Close The returned function type by Ch FuncTypeQ A generic pointer to a binary interface function for Ch DeclareFunc Options for setting embedded Ch as an argument of Ch Initialize A generic pointer type for Ch SetGlobalUserData and Ch GetGlobalUserData A generic pointer type for the tag of a user defined struct class union type A structure containing the information for a user defined struct class union A structure containing the information for a member of a user defined type A generic pointer for an internal value of an expression for Ch ExprValue and Ch DeleteExpr Value The returned variable type by Ch VarType Type ChOptions t is a structure to set the option of Embedded Ch by by Ch Initialize typedef struct ChOptions_ int shelltype char chhome ChOptions t shell type Embedded Ch home directory Type ChBlock t is a structure containing the information for a function block used by Ch AddCallback and Ch ChangeStack 199 Chapter A APIs for Embedding Ch lt embedch h gt typedef struct ChBlock
143. H PROGRAMS IN C SPACE 1 2 BUILDING EXECUTABLES IN WINDOWS for applications compiled with option MD are located in the directory C Ch extern lib which should be added to the search path for libraries in linking the executable program If an application is com piled with option MDd for debug multi thread dynamically linked system libraries libraries chsdk mdd lib and embedch mdd lib should be used If an application is compiled with option MT for multi thread static linked system libraries libraries chsdk mt 1ib and embedch mt 1ib should be used If an application is compiled with option MTd for debug multi thread static linked system libraries libraries chsdk mtd lib and embedchomtd 1ib should be used For some applications the system library advapi32 1ib may also need to be linked to create an executable application program The startup file chrc in the user s home directory can be configured to setup the Visual C VC in Windows for automatic compilation using a Makefile in a Ch shell This startup file can be created by running the following command in a command shell ch d If a VC in NET 2005 is installed at C Program Files Microsoft Visual Studio 8 the code below needs to be added to the startup file chrc in the user s home directory _path stradd _path C Program Files Microsoft Visual Studio 8 VC bin _path stradd path C Program Files Microsoft Visual Studio 8 COMMON7 IDE putenv stradd
144. H SPACE 4 3 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE WITH DL FILE Ch Space embedch3 ch C Space A va ES embedch3 exe chfun lt int c_var 10 dlrunfunc int main o o I I I I I l I I k I EN H Ch CallFuncByName interp chfun NULL I E I I I I I I I I I o o o libembedch3 dl extern inte var EXPORTCH int embedch3 chdl o return c var M Z Figure 4 2 Accessing Variables in C Space from Ch Space 74 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 3 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE WITH DL FILE include lt stdio h gt include lt string h gt include lt embedch h gt EXPORTCH int c var 10 int main ChInterp t interp int status char argvv embedch3 ch NULL initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program embedch3 ch failed n printf in embedch3 c amp c var p c var d n amp c var c var Ch CallFuncByName interp chfun NULL printf in embedch3 c amp c var p c var d n amp c var c var c var 100 change the variable value in C space printf nin embedch3 c amp c var p c var d n amp c var c var Ch CallFunc
145. HHOME toolkit demos embedch for different compilers and options It is recommended that you try these examples while reading this document 1 4 Getting Started In Ch SDK Software Development Kit User s Guide how to call C C functions from Ch scripts has been described Ch SDK can be used to build interface including wrapper functions and dynamically loaded library in dl file between Ch and C C when a Ch program runs first The Ch program then calls C C functions in binary library In this case a Dynamically Loaded Libraries DLLs with binary C C functions is loaded first by function dlopen Function dlopen follows the Unix POSIX standard It is a standard C function in Linux It is equivalent to LoadLibrary in Windows Then the address of a function is obtained by function dlsym Finally the function is invoked by function dlrunfun With Embedded Ch typically a binary application program is launched first The program invokes Ch scritps C C scripts through an Embedded Ch scripting engine When a Ch scripting engine is embedded in a C C executable program the binary library functions and Ch scripting engine are part of the same application The addresses of variables in both C C and Ch spaces can be shared each other Therefore it is much simpler and easier to call C C functions from Ch space or call Ch script functions from C C space Embedded Ch provides macros and core APIs Ch Initialize Ch RunScript
146. In Ch User s Guide it is described that arrays with different data type can be handled by the same function with argument of array of reference This feature cannot apply to embedded Ch The array in C space should be the same data type as the argument of the Ch function to be called In other words only arrays of type int can be passed to function funct above Program 6 3 is another version of Ch function sum2d which takes an argument of array of reference with the fixed dimension The C program in Program 62 still works with this Ch function The output is the same as Figure 6 1 If the Ch function takes the argument of an array of reference without constraint of dimension arrays with different dimensions can be passed to it Assume that the Ch function funct defined below takes two one dimensional assumed shape arrays as arguments int funct array int amp a array int sb After the Ch program is loaded and executed function funct can be called in C space as follows int dim a ext a dim b ext b 1 ext b 2 int retval int aal 3 1 2 3 int bb1 2 2 4 5 6 Th X dim a 1 ext a 3 dimension and extent of aal dim b 2 ext b 1 2 ext b 2 2 dimension and extents of bb1 Ch CallFuncByName interp funct amp retval aal dim a ext a bbl dim b ext b 1 ext b 2 120 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COM
147. In function chPrintSymbolValue all variables and their values are printed out except for func tion prototypes without function definition However to build a GUI debuger one may suppress the display of member function definitions constructor destructor of a class function definitions built in variables func class class name and Ch specific global variables Chlibname handle for han dling functions in libraries 158 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES tag tag is called tag tag is called tag tag is called tag funcl called argument i in funcl is true event MASKEND is true The stack level is 0 The current line number is 33 Member function tag funcl starts at line 27 Member function tag funcl ends at line 35 Member function tag funcl Class name is tag The current file name is cplus cpp Member Function tag funcl at stack level 0 has 12 symbols d 30 z 0 func funcl __class__ tag __class_func__ tag funcl i 50 y 0 tag constructor tag destructor funcl member function this 0X51a58 j 10 The stack level is 1 The current line number is 43 Function main starts at line 38 Function main ends at line 45 The function name is main The current file name is cplus cpp Function main at stack level 1 has 5 symbols 2 fune main i 10 x 0 e i 50 y 0 tag constructor tag destructor funcl member
148. In this chapter we will describe these methods in detail 3 1 Calling Ch Functions by Expression Evaluations In addition to regular expressions Ch function calls that appear in the form of expressions can also be handled by functions Ch ExprEval and Ch ExprCalc Using Ch ExprCalc to evaluate an expression which may contain functions is quite simple as illustrated in Program 2 12 C program uncall cinProgram 3 Tlis an example of calling Ch functions by expression evaluations The Ch program uncall ch shown in Program 3 2 has three global variables ii with type int dd with type double and retval g with type int as well as a Ch function func Function func takes an argument argl of type int and returns an integer with value of 2 argl It also prints out the current values of variables ii and dd To get the return value of the Ch function in C space the variables pretval g is declared as a pointer to int in C program funcall c After the Ch program B 2Jis loaded and executed by funcall c with embedded Ch the statement below pretval g Ch SymbolAddrByName interp retval g obtain addresses of variable retval g declared in Ch program and pretval g is used to keep this ad dress The statement Ch ExprEval interp retval g func 10 evaluates the expression retval g func 10 which actually is a function call in Ch space The return value is saved in Ch variable retval g which is pointed to by the C pointer pretv
149. KCKCKCkCKCkCKCkCKCkCKCKCKCkCKCkCKCKCKCKCKCKCKCkCKCkCKCKCKCkCKCk Ck kk k ck k ck k ck kok ck kk kk kk ke ke ke ex include lt stdio h gt include lt string h gt include lt embedch h gt struct tag int i double f sp int main ChInterp t interp int status char argvv expreval ch NULL int pii p2 pa double pdd initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program expreval ch failed n pii Ch SymbolAddrByName interp ii pdd Ch SymbolAddrByName interp dd p2 Ch SymbolAddrByName interp p pa int Ch SymbolAddrByName interp a sp Ch SymbolAddrByName interp s printf In C befor xpression evaluations Mn printf ii d dd Sf p dMn pii pdd p2 printf a 0 Sd a l d a 2 d n pa pa 1 pa 2 printf s i d s f f n sp gt i sp gt f Ch ExprEval interp ii Ch ExprEval interp dd dd ii 2 1 pow 2 0 3 Ch ExprEval interp func printf In C after expression evaluations Mn printf ii d dd Sf p dMn pii pdd p2 Ch End interp Program 2 10 Evaluating expressions in a Ch program expreval c 41 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS int ii 10
150. KR KKK A KG A X e X e x A x application ck ko ke e e eoe ee eee x x f int ii 10 double dd 1 1 void fun printf in fun in exprcalc ch ii d dd f n ii dd chexprcalc Output ii 10 dd 1 100000 in fun in exprcalc ch ii 10 dd 1 100000 After dd dd ii ii 10 dd 11 100000 result 11 100000 ii 2 20 000000 in fun in exprcalc ch ii 10 dd 11 100000 hypot 3 4 5 000000 See Also Ch SymbolAddrByName Ch DataType Ch CallFuncByAddr Ch CallFuncByName Ch ExprParse Ch ExprValue Ch DeleteExpr Value 250 Chapter A APIs for Embedding Ch lt embedch h gt Ch ExprEval Ch ExprEval Synopsis include lt ch h gt int Ch_ExprEval ChInterp_t interp const char expr Purpose Execute an expression in Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter expr Expression to be executed in Ch space Description The function Ch_ExprEval executes an expression in Ch space It can be used to evaluate an expression or call a function in Ch space The expression expr may invoke functions located in function files specified by the function file path _fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is an operand of an expressi
151. LATYP printf a2 is a C VLA array n printf Array dim of a2 is d n Ch ArrayDim interp a2 printf Array elements of a2 is d n Ch ArrayNum interp a2 GI printf Extent of the 1st dim of a2 d n Ch ArrayExtent interp a2 0 printf Ch_DataSize interp a2 dMn Ch DataSize interp a2 if Ch_DataType interp pa CH INTPTRTYPE if Ch_ArrayType interp pa CH UNDEFINETYPE printf pa is not an array n printf pa is a pointer to int n ap int Ch SymbolAddrByName interp pa printf ap p n ap 230 Chapter A APIs for Embedding Ch lt embedch h gt if Ch DataType interp func CH FLOATTYP printf func return type is float n r Ch_DataType if Ch_DataType interp func 3 0 CH_DOUBLETYPE printf func 3 0 is double type n or if Ch_FuncType interp func CH FUNCTYPE if Ch_FuncType interp func printf func is a function n printf Number of arguments of function func is d n Ch_FuncArgNum interp func if Ch_FuncType interp funcv CH FUNCPROTOTYPE printf funcv is a function prototype n printf Number of arguments of function funcv Ch FuncArgNum interp funcv is if Ch_IsFuncVarArg interp funcv printf funcv is a function of variable argumen if Ch_FuncType interp fun
152. LING C C FUNCTIONS FROM CH SPACE 5 2 CALLING C FUNCTIONS IN MAIN PROGRAM FROM CH WITHOUT FUNCTION FILES assume CHHOME is C ch If not change C ch to CHHOME such as C usr local ch CC cl LD cl INC IC ch extern include I CFLAG MD FLAG C ch extern lib chsdk lib X C ch extern lib embedch lib advapi32 lib target prog exe progl exe prog exe prog obj func obj sample chdl obj S LD o prog exe prog obj func obj sample chdl obj LFLAG prog obj prog c CC c prog c INC CFLAG progl exe progl obj func obj sample chdl obj LD o progl exe progl obj func obj sample chdl obj LFLAG progl obj progl c CC c progl c INC CFLAG func obj func c CC c func c INC CFLAG sample_chdl obj sample_chdl c CC c sample chdl c INC CFLAG clean rm f o obj exe lib exp Program 5 11 Makefile to create prog exe for Windows Makefile Win The source code for examples described in this section are available in the directory CHHOME toolkit demos embedch chapters chapter5 embedfuncmain 5 2 Calling C Functions in Main Program from Ch Without Function Files In section 5 1 functions funcl and func2 are treated as regular functions using function files To simplify the interface to the binary C function these two functions can be treated as system functions in the Ch space and defined using the API Ch DeclareFunc with the following function calls Ch D
153. ND is true The stack level is O0 The current number is 16 Function funcl starts at line 7 Function funcl ends at line 20 The function name is funcl is true lin The current file name is prog5 ch i y 1500 2 i r g 760 000000 The stack level is 1 The current line number is 25 Function main starts at line 22 Function main ends at line 30 The function name is main The current file name is prog5 ch i x 500 2 i r g 620 000000 i in funcli xs 10 y in funcl is 50 g in funcl is 500 000000 x in main is 50 g in main is 100 000000 r in main is 500 000000 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES Figure 7 9 Output from executing prog5 c 165 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER Ck ck ck k ck k ck ck k k ck k k k ck ck kk kk KKK Debug Menu KKKKKKKKKKKKKKKKKKKKK KKK load filename load filename for debugging start args start the program with debugging run args run the program without debugging step step into a function or next line next step over a function or next line cont continue till hitting a breakpoint or ends up change stack to the calling function down change stack to the called function stack display stack names in all stacks locals display variables and values within its scope variables display variables and values in all s
154. OM CH SPACE WITH DL FILE target libsampclass dl libsampclass dl sampclass chdl obj ch dllink libsampclass dl cplusplus sampclass chdl obj prog lib sampclass chdl obj sampclass chdl cpp ch dlcomp libsampclass dl cplusplus sampclass chdl cpp clean del obj dl exe lib exp Program 5 45 Makefile to create libsampclass dl for Windows Makefile dl 115 Chapter 6 Calling Special Ch Functions from C Space In chapters 2 and 3 we have described how to access Ch variables and call Ch functions in Ch pro grams that are executed by C programs with embedded Ch Some commonly used APIs including Ch SymbolAddrByName Ch CallFuncByAddr Ch CallFuncByName Ch CallFuncByNameVar and Ch ExprEval have been introduced with simple samples Some variable length arrays VLAs com putational arrays and string type st ring_t are available only in Ch How to call these special Ch functions involving these data types from Ch space is described in this chapter 6 1 Calling Ch Functions with VLAs and Computational Arrays from C Space Calling Ch functions with VLAs from C space or with return type of computational array is described in this section 6 1 1 Calling Ch Functions with Arguments of VLAs Calling Ch functions with arguments of VLAs will be described in this section In C space an array is passed to a function as a pointer only For the C function with arguments of fixed length arrays such as int functl
155. PE printf meminfo gt dtype CH INTPTRTYPE 1n printf meminfo gt ispublic d n meminfo gt ispublic if meminfo gt isfunc printf meminfo gt isfunc d n meminfo gt isfunc if meminfo gt isconstructor printf meminfo gt isconstructor dWMn meminfo gt isconstructor if meminfo isdestructor printf meminfo gt isdestructor d n meminfo gt isdestructor if meminfo gt isvararg printf meminfo gt isvararg din meminfo gt isvararg if meminfo gt arraytype printf meminfo gt arraytype d n meminfo gt arraytype printf meminfo gt dim d n meminfo gt dim printf meminfo gt externt 0 dWMn meminfo gt extent 0 printf meminfo gt externt 1 dWMn meminfo gt extent 1 if meminfo gt isbitfield printf meminfo gt isbitfield d n meminfo gt isbitfield printf meminfo gt fieldsize d n meminfo gt fieldsize printf meminfo gt fieldoffset d n meminfo gt fieldoffset if meminfo gt dtype CH_STRUCTTYPE meminfo gt dtype CH CLASSTYPE meminfo gt dtype CH UNIONTYPE printf meminfo gt udtag p n meminfo gt udtag printf Nn return 0 Program 2 9 C program for obtaining information for variables of user defined type userdefinedsymbol c Contd 39 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS amp s 50090 amp s i
156. POINTLINE fr bp u file filename lse if bp btype ACTION SETBREAKPOINTFUNC free bp gt u func funcname lse if bp gt btype ACTION SETBREAKPOINTVAR free bp gt u var varname if bp u var varvalue free bp u var varvalue if bp gt condexpr free bp gt condexpr if bp gt condvalue free bp gt condvalue free bp dgptr gt bkpt NULL return 0 int chAssign debugInfo_t dgptr char expr ChInterp t interp int retval interp dgptr gt interp if interp NULL Ch ExprParse interp expr CH ERROR return 1 retval Ch ExprEval interp expr return retval int chCall debugInfo t dgptr char expr ChInterp t interp int retval interp dgptr gt interp if interp NULL Ch ExprParse interp expr CH ERROR return 1 retval Ch ExprEval interp expr return retval Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 189 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chPrint debugInfo t dgptr char expr ChInterp t interp ChValueNode t valuenode void addr int retval 0 interp dgptr gt interp valuenode Ch ExprValue interp expr amp addr if valuenode NULL chPrintSymbolValue interp expr addr Ch DeleteExprValue interp valuenode else retval 1 re
157. PUTATIONAL ARRAYS FROM C SPACE P z PB To call the Ch function from C space the users needs to provide not only information of extents like what we have done in the previous section but also the number of dimensions The array aa1 is a one dimension array with extent of 3 whereas bb1 is a two dimension array with extents of 2 and 2 In the argument list of Ch CallFuncByName Extra arguments dim a ext_a dim b ext b 1 and ext b 2 which contains number of dimensions and extents of aa1 and bb1 have been added and passed to the Ch function funct The complete sample of this case is shown in Programs 6 4 and 6 5 The Ch function sumNd in Program 6 4 iis designed to calculate the sum of each element of array a with different dimension The statement aa array double totnum a casts array a which can have any dimensions to the one dimensional array aa with totnum elements In C space Program 6 5 this Ch function is called twice to calculate the sum of two arrays of different dimensions For the array aa1 which has the shape of 3 x 4 three extra arguments dim ext1 and ext 2 which represent dimensions and extents are added after the argument aa1 It is shown as follows dim 2 extl 3 ext2 4 dimension and extents of aal Ch CallFuncByName interp sumNd amp sum ret aal dim extl ext2 For the array aa2 which has the shape of 2 x 3 x 4 four extra arguments the dimension dim and ex
158. Pointer t clientdata akpoint if calldata event amp CH MASKRET return from a function while bp if bp btype ACTION SETBREAKPOINTFUNC if calldata gt funcname NULL amp amp bp gt u func called TRUE amp amp return from this function strcmp bp u func funcname bp gt u func called FALSE bp bp gt next dgptr bkpt for step over functions if dgptr gt runmode RUNMOD if calldata gt event amp CH else if calldata gt event 4 CH_MASKR if dgptr gt stepovernum gt 0 E STEPOVER ASKCALL dgptr gt stepovernum 1 dgptr gt stepovernum 1 calldata funcname reset step over a new function ET returning from a function stepped over executing statements inside a function stepped over if dgptr stepovernum 0 return Program 7 17 The callback for the debug program callback c 196 Chapter APIs for Embedding Ch lt embedch h gt for start step into step over and cont if calldata event amp CH MASKLINE if dgptr gt runmode RUNMODE STEPINTO dgptr gt runmode RUNMODE STEPOVER chProcessWatchExprs dgptr chProcessActionMenu dgptr return process the list of breakpoints while bp if bp gt btype ACTION SETBREAKPOINTLINE if calldata gt source NULL amp a
159. Program 7 17 is the central component for the debugger program The registered client data by the API Ch AddCallback points to the debugger Statements 171 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER dgptr debugInfo t clientdata dgptr gt calldata calldata dgptr gt level calldata gt level retrieves the debugger first The calldata and stack level are then saved in the debugger The breakpoints in the linked list of the debugger is used inside the callback function If the callback is triggered by the function return event it is first checked if the returning function had just been stopped by a breakpoint for function If this is the case the field called for this breakpoint is reset to 0 by the statement bp gt u func called FALSE so that the program will stop at this function again when it is called If the callback is triggered by the next command to step over a function or a statement the conditional expression of the if statement if dgptr runmode RUNMODE STEPOVER will be true If the callback is triggered by the function call event the member dgpt r stepovernum will increment If the callback is triggered by the function return event the member dgptr stepovernum will decrement If the value of dgpt r gt stepovernum is not zero the call back is triggered by a statement inside a function stepped over Therefore the flow of the program control returns from t
160. RAYPTRTYPE printf arg 2 of 5 is pointer to C array n if Ch_FuncArgArrayType interp f5 2 CH CHARRAYVLATYPE printf arg 3 of 5 is Ch VLA array n if Ch_FuncArgArrayType interp f5 3 CH CHARRAYVLATYPE printf arg 4 of 5 is Ch VLA array n if Ch FuncArgArrayType interp f5 4 CH CHARRAYVLATYPE printf arg 5 of 5 is Ch VLA array n if Ch FuncArgArrayType interp f5 5 CH CHARRAYVLATYPE printf arg 6 of 5 is Ch VLA array n if Ch_FuncArgArrayType interp f6 6 CH CARRAYPTRTYPE printf arg 7 of f6 is pointer to C array n if Ch FuncArgArrayType interp f8 0 CH CHARRAYTYPE printf arg 1 of 8 is Ch array n if Ch FuncArgArrayType interp f8 1 CH CHARRAYVLATYPE printf arg 2 of 8 is Ch VLA array n if Ch FuncArgArrayType interp f8 2 CH CHARRAYVLATYPE printf arg 3 of 8 is Ch VLA array n printf number of elements of arg 1 of fl is d n Ch FuncArgArrayNum interp f1 0 printf number of elements of arg 4 of f1 is d n Ch FuncArgArrayNum interp f1 3 printf number of elements of arg 1 of f2 is d n Ch FuncArgArrayNum interp f2 0 printf number of elements of arg 2 of f4 is d n Ch FuncArgArrayNum interp f4 1 printf number of elements of arg 3 of f4 is d n Ch FuncArgArrayNum interp f4 2 printf number of elements of arg 4 of f4 is d n Ch FuncArgArrayNum interp f4 3 printf number of elements of arg 5 of f4 is d n Ch FuncArgAr
161. S FROM C SPACE func called n2 m 3 f 10 000000 p 2 1 1 5 1 2 6 2 3 5 6 2 5 1 3 6 10 f 20 000000 nc called 2 3 1 E V U BE OMAR HF OTM nmnmso5nutuimn osnDnDauymcogv 10 000000 p 2 1 1 5 1 2 6 2 3 5 6 23 5 6 i 10 sp gt f 20 000000 3 5 CALLING CH FUNCTIONS BY CH CALLFUNCBYNAMEVARO Figure 3 4 Output from executing call funcbynamevar c 59 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS These two function calls in Program 3 8 generate the same output shown in Figure 3 4 In Program 3 8 after a variable argument list in the Ch space is created by function Ch VarArgsCreate arguments of the called function is added to the list by either function function Ch VarArgsAddArg or Ch VarArgsAddArgExpr Then the function func in the Ch space is called by Ch CallFuncByNameVar Finally the variable argument list is deleted by Ch VarArgsDelete Function Ch VarA rgsA ddArgExpr adds an expression in the Ch space as an augment to the argument list Therefore variables in an expression shall be declared in a Ch program before this Ch VarArgsAddArgExpr is called Function Ch VarArgsAddArg adds an expression in the C space as an augment to the argument list No variable in the Ch space needs to be explicitly declared In a typical application the argument list arglist is typically built dyn
162. Script Ch ExecScript Synopsis include lt embedch h gt int Ch ExecScript ChInterp t interp const char progname Purpose Execute a Ch program parsed by function Ch ParseScript Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter programe The program name the first element of the second argument of array of pointer to char in function Ch ParseScript Description When a program is run in Ch it first will be parsed then executed The function Ch ExecScript executes a Ch program parsed by function Ch ParseScript in C space The variable progname contains the file name of the Ch program parsed by function Ch ParseScript The API Ch Initialize and Ch ParseScript should be called before this function is called After function Ch ExecScript is called variables and functions inside the script can be accessed by other APIs in the same interpreter interp However after function Ch ExecScriptM is called variables and functions inside the script cannot be accessed by other APIs in the same interpreter interp Example Refer to Program I 4 See Also Ch AppendParseScript Ch AppendRunScript Ch ExecScriptM Ch ParseScript Ch RunScript Ch Initialize Ch Home Ch SymbolAddrByName Ch DataType Ch ExprEval Ch End 246 Chapter A APIs for Embedding Ch lt embedch h gt Ch ExecScriptM Ch ExecScriptM Synopsis includ
163. See Also Ch FuncArgDataType Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgIsFunc Ch FuncArglIsFuncVarArg Ch FuncArgFuncArsNum Ch FuncArgUserDefinedName Ch FuncArgUserDefinedSize 260 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgDataType Ch FuncArgDataType Synopsis include lt ch h gt ChType_t Ch FuncArgDataType ChInterp t interp const char funcname int argnum Purpose Get the data type for an argument of a function in a Ch program Return Value This function returns a macro for data type Ch Type t defined in header file ch h described in Ch SDK User s Guide If a passed argument is of array type the function will return the type of the element of the array For example CH INTTYPE and CH DOUBLETYPE are two macros defined for data type of int and double in Ch Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description The function Ch FuncArgDataType gets the data type for an argument of a function in the Ch space If a variable of pointer to computational array it gives the data type of its element Unlike function Ch FuncArgDataType For a pointer to C array it also gives data type of its element so that the complete information for an argument can be obtained Example In this example information about arguments of functions in the emb
164. Settlnfegratio The Ch Language Environment Version 6 1 Embedded Ch SDK User s Guide How to Contact SoftIntegration Mail SoftIntegration Inc 216 F Street 68 Davis CA 95616 Phone 1 530 297 7398 Fax 1530 297 7392 Web http www softintegration com Email info softintegration com Copyright 2001 2008 by SoftIntegration Inc All rights reserved Revision 6 1 September 2008 No part of this publication may be reproduced stored in a retrieval system or transmitted in any form or by any means electronic mechanical photocopying recording or otherwise without the prior written permission of the copyright holder SoftIntegration Inc is the holder of the copyright to the Ch language environment described in this docu ment including without limitation such aspects of the system as its code structure sequence organization programming language header files function and command files object modules static and dynamic loaded libraries of object modules compilation of command and library names interface with other languages and object modules of static and dynamic libraries Use of the system unless pursuant to the terms of a license granted by SoftIntegration or as otherwise authorized by law is an infringement of the copyright SoftIntegration Inc makes no representations expressed or implied with respect to this documenta tion or the software it describes including without limitations any implied warrant
165. The program embedch2 ch executed by Ch RunScript remains inside the memory so that other Embedded Ch APIs such as Ch_CallFuncByName can be used to access global variables and functions before the interpreter 17 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS is terminated by Ch End Typical scenarios for running embedded Ch programs in a single thread application are as follows e Ch Initialize amp interp option while 1 Ch RunScriptM interp argv Ch End interp e Ch Initialize amp interp option while 1 Ch ParseScript interp argv Ch ExecScriptM interp argv 0 Ch End interp e Ch Initialize amp interp option while 1 Ch RunScriptM interp argvl Ch ParseScript interp argv2 Ch ExecScriptM interp argv2 0 Ch End interp e Ch Initialize amp interp option while 1 Ch ParseScript interp argv other API such as Ch CallFuncByName to access variables and functions in script argv 0 Ch End interp e Ch Initialize amp interp option while 1 Ch ParseScript interp argv Ch AppendParseScript interp code Ch AppendParseScriptFile interp filename other API such as Ch CallFuncByName to access variables and functions in script argv 0 code and filename Ch End interp e Ch Initialize amp interp option while 1 Ch AppendParseScript interp code
166. Using C The above sample debugger has been ported to C The sample code is available at CHHOME toolkit demos embedch chapters chapter7 cpp directory when Embedded Ch is installed 173 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER File debug h For the run time debug ifndef DEBUG H define DEBUG H include embedch h ifdef _ cplusplus extern C endif define boolean value define TRUE 1 define FALSE 0 define MAX_PROMPT_STR 2048 maximum prompt string length action from the user through a GUI typedef enum ACTION_UNDEFINED for debug commands or menus in a GUI ACTION_LOAD command load filename ACTION_START command start args with debugging ACTION_RUN command run args without debugging ACTION_STEPINTO command step into ACTION_STEPOVER command next to step over ACTION_CONTINUE command cont to continue ACTION_UPSTACK command up function stack ACTION_DOWNSTACK command down function stack ACTION_STACK command stack function names ACTION_LOCALS command locals variables and values ACTION_VARIABLES command variables variables and values in stacks ACTION_ADDWATCHEXPR command watch to add watch expr ACTION_REMOVEWATCHEXPR command remov xpr remove a watch expr
167. YPE printf arg 3 of f3 is char n if Ch FuncArgDataType interp f4 1 CH INTTYPE printf arg 2 of f4 is int Mn if Ch FuncArgDataType interp f4 2 CH INTTYPE printf arg 3 of f4 is int Wn if Ch FuncArgDataType interp f4 3 CH INTTYPE printf arg 4 of f4 is int Wn if Ch FuncArgDataType interp f4 4 CH INTTYPE printf arg 5 of f4 is int Wn if Ch FuncArgDataType interp f5 1 CH INTTYPE printf arg 2 of f5 is int Mn if Ch FuncArgDataType interp f5 2 CH INTTYPE printf arg 3 of f5 is int Wn if Ch FuncArgDataType interp f5 3 CH INTTYPE printf arg 4 of f5 is int Wn if Ch FuncArgDataType interp f5 4 CH INTTYPE printf arg 5 of f5 is int Wn if Ch FuncArgDataType interp f5 5 CH INTTYPE printf arg 6 of f5 is int Wn if Ch_FuncArgDataType interp f6 0 CH STRUCTTYPE printf arg 1 of 6 is struct Mn if Ch FuncArgDataType interp f6 1 CH STRUCTPTRTYPE printf arg 2 of f6 is struct Mn if Ch FuncArgDataType interp f6 2 CH CLASSTYPE printf arg 3 of 6 is class Mn if Ch FuncArgDataType interp f6 3 CH CLASSPTRTYPE printf arg 4 of 6 is class Mn if Ch FuncArgDataType interp f6 4 CH UNIONTYPE printf arg 5 of f6 is union Mn if Ch FuncArgDataType interp f6 5 CH UNIONPTRTYPE printf arg 6 of f6 is union Mn if Ch_FuncArgDataType interp f6 6 CH STRUCTTYPE printf arg 7 of
168. _SymbolTotalNum interp for index 0 index lt totalnum index t name Ch SymbolNameByIndex interp index if Ch FuncType interp name varnum tt if Ch VarType interp localvariable printf localvariable is not CH_LOCALVARTYPE or CH_GLOBALVARTYPE n if Ch_VarType interp func CH GLOBALVARTYPE printf func is CH_GLOBALVARTYPE n return varnum int main ChInterp t interp int status char argvv symbol ch NULL int index totalnum Ch Initialize amp interp NULL status Ch RunScript interp argvv or status Ch ParseScript interp argvv if status CH ERROR printf Error execution of program symbol ch failed Wn index Ch SymbolIndexByName interp d printf symbol index for d is d n index index Ch SymbolIndexByName interp unknown printf symbol index for unknown is d n index totalnum Ch SymbolTotalNum interp printf The total number of symbols is d n totalnum for index 0 index lt totalnum index printf symbol index d name s address Ch SymbolNameByIndex interp index Ch SymbolAddrByIndex interp index p n index printf Ch_SymbolAddrByName interp main p n Ch SymbolAddrByName interp main printf Number of global variables of not function type is dWMn numOfVar interp Ch End interp Program 2 4 Accessing symbols in the symbol table
169. a variable number of arguments Return Value If the argument is a function or pointer to function with a variable number of arguments this function returns 1 Otherwise it returns 0 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description The function Ch FuncArgIsFuncVarArg determines if the argument is a function or pointer to function with a variable number of argument Example See Ch_FuncArgDataType See Also Ch_FuncArgDataType Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgArrayType Ch FuncArgIsFunc Ch FuncArgFuncArgNum Ch FuncArgUserDefinedName Ch FuncA rgUserDefinedSize 271 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgNum Ch FuncArgNum Synopsis include lt ch h gt int Ch FuncArgNum ChlInterp t interp const char name Purpose Obtain the number of the arguments of the function pointer to function or member function of class Return Value This function returns the number of the arguments of the function If the argument is not function type it returns 1 Parameters interp A Ch interpreter name Name of the variable in the Ch space Description This function gets the number of the arguments of a function pointer to function or member function of a class If the function takes a variable number of arguments it
170. acters of s 10 abc size printValue interp memtype memaddr meminfo udtag nestlevel memaddr char memaddr size if datatype CH CHARTYPE printf y else if meminfo dim 2 for i 0 i lt meminfo extent 0 i if i 0 printf l6g for j 0 j lt meminfo extent 1 j Size printValue interp meminfo dtype memaddr meminfo udtag nestlevel memaddr char memaddr size printf if i meminfo extent 0 1 printf ANHE else printValue interp memtype memaddr meminfo udtag nestlevel return udinfo size Program 7 9 Functions for printing values of all variables in a stack stack c Contd 152 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES int printValue ChInterp t interp ChType t datatype void addr ChUserDefinedTag t udtag int nestlevel int size 0 Note all data types are implemented in the distributed stack c switch datatype case CH CHARTYPE printf c char addr size sizeof char break case CH INTTYPE printf d int addr size sizeof int break case CH FLOATTYPE printf 2f float addr size sizeof float break case CH DOUBLETYPE printf 4f double addr size sizeof double break case CH LCOMPLEXTYPE printf 4f 1i5 4 double addr double addr 1 size 2 sizeof double
171. ad prog6 ch debug gt start optl opt2 will pass the arguments prog6 ch opt1 and opt2 to argv 0 argv 1 and argv 2 of the main function int main int argc char argv of the Ch script prog6 ch respectively An optional argument with space should be enclosed within two double quotation marks as shown below debug start optl opt2 with space opt3 If the user types command step next or cont before the program has started the function chStart will be called The argument runmode of function chStart therefore can have a different execution command which is saved in the member runmode of the debugger Function chStart starts the Embedded Ch by calling the API Ch Initialize The instance of the interpreter is saved as a member of the debugger Before the program is executed by the APIs Ch ParseScript and Ch ExecScript the callback function with the new line event mask is added by the API Ch AddCallback Typically the user needs to load a program and set breakpoints before starting a program To execute the loaded program without debugging or executing the program in the mid of the debugging the user can type command run The corresponding function chRun will be called It unregisters the callback function by the event mask Ch MASKNONE If the loaded program is already started it just changes the event mask of the callback function by calling the API Ch AddCallback only Otherwise a new instance of Embedded Ch nee
172. added in C99 The value for the local variable i inside the block is printed out Afterwards the condition expression Ch ChangeStack interp level amp calldata2 CH OK of the while loop in function callback changes the function stack recursively The stack level for the active function unc1 is 0 The stack level for the calling function main is 1 which is avail able as a value for the member level of the variable call data2 of structure ChBlock t Functions printCallData and symbols will print out the information and symbols _func_ i and x in function main The value for the variable i in the calling function main is 10 The next call of function Ch ChangeStack in the while loop will change the stack out of the function to the program scope The stack level will be increased to 2 At the program scope the line number is not available In this case the line number becomes 0 by default There are 5 symbols i g a funcl and main in the program scope The value for the global variable i is 40 The subsequent call of function Ch ChangeStack with the value 3 for the stack level will return the value CH INVALIDLEVEL which indicates that the specified stack level is invalid 138 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK funcl even The stack level is called argument i in funcl t MASKEND is true is true is 0 Th Func Func The Th current lin
173. ain vcproj for examples described in sections 5 T and 5 5 can be found in the distribu tion of Embedded Ch for Windows in the directories CHHOME toolkit demos embedch chapters chapter5 embedfuncmain and CHHOME toolkit demos embedch chapters chapter5 embedclassmain respectively They can be opened from Visual NET directly Please check CHHOME toolkit demos embedch chapters chapter5 embedfuncmain readme and CHHOME toolkit demos embedch chapters chapter5 embedclassmain readme for more details on how to run the program CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 2 BUILDING EXECUTABLES IN WINDOWS 1 2 3 Building Executables in Windows Using Visual C 6 0 Assume Ch is installed in the directory C X Ch To compile the code using Embedded Ch SDK in VC 6 0 follow the procedures below e Create a Win32 project or Win32 console project e Header files ch h and embedch h are located in the directory CHHOME extern include Some sample programs described in this document use header files in the current working directory Add the directory CHHOME extern include and the current working directory to the header file search path Click Tools gt Options it will pop up an Option window Click Directories se lect Win32 under plat form and select Include files under Show directories for Finally add C Ch extern include under Directories e The application program should be linked with Embedded Ch libraries chsdk
174. al interp printf g d n g ExecScript interp argvv 0 End interp CQ Q OQ Or Aa O h ho h he rintf Xncase 2 Ch InitGlobalVar interp 0 n h_Initialize amp interp NULL h ParseScript interp argvv ExecScript interp argvv 0 CallFuncByName interp func NULL ExprEval interp printf g d n g End interp Q OQ A A A 0 h h hc hc rintf Xncase 3 Ch InitGlobalVar interp 1 n h_Initialize amp interp NULL h_InitGlobalVar interp 1 h_ParseScript interp argvv h CallFuncByName interp func NULL h ExprEval interp printf g d n g h ExecScript interp argvv 0 h End interp return 0 Q QO OO A Q g initglobalvar ch A Ch program with initialization of global variables include lt stdio h gt class tag public tag tag void member1 void member2 l tag tag printf tag tag called n tag tag printf tag tag called n void tag memberl printf tag memberl called n void tag member2 printf tag member2 called n class tag c int g 10 c member2 284 Ch InitGlobalVar Chapter A APIs for Embedding Ch lt embedch h gt void func c memberl Output case 1 Ch_InitGlobalVar interp 0 tag memberl called g 0 tag tag called tag member2 called tag
175. al g After Ch variables ii and dd are changed by the following expression evaluations Ch ExprEval interp ii Ch ExprEval interp dd dd ii the Ch function func is invoked again by 47 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 1 CALLING CH FUNCTIONS BY EXPRESSION EVALUATIONS KK kk kk kA kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kok k k k k Ck k k kk ck k ck kc kok ck ck I OK File Name funcall c Calling Ch functions by evaluating expressions CKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCkCKCkCk kk k ck k k ck kok ck kk k k ok ke ke ke x include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv funcall ch NULL int pretval_g initialize embedded Ch Ch Initialize amp interp NULL parse embedded Ch program indicated by argvv status Ch ParseScript interp argvv if status CH ERROR printf Error parse program funcall ch failed n else execut mbedded Ch program funcall ch status Ch ExecScript interp argvv 0 if status printf Error execute script funcall ch failed n pretval g Ch SymbolAddrByName interp retval g printf In C the original value of pretval_g n printf pretval g d n pretval g Ch ExprEval interp retval g func 10 call Ch function func printf In C after the
176. alSymbolAddrByName Ch SymbolAddrByIndex Ch_VarType 279 Ch_GlobalSymbolAddrByIndex Ch_GlobalSymbolIndexByName Ch_GlobalSymbolNameByIndex Chapter A APIs for Embedding Ch lt embedch h gt Ch GlobalSymbolIndexByName Ch GlobalSymbolIndexByName Synopsis include lt ch h gt int Ch_GlobalSymbolIndexByName ChInterp t interp const char name Purpose Get the index number of a global variable in the symbol table of a Ch program Return Value The index number of a global variable in the symbol table of a Ch program If the name is not in the list of the symbol table the function returns 1 Parameters interp A Ch interpreter name The symbol name of a global variable in a Ch program Description The function Ch_GlobalSymbolIndexByName obtains the index number of a global variable in the symbol table in a Ch program inside a dynamically loaded library or embedded Ch program The index number for the symbol table starts with 0 Example See Ch_GlobalSymbolAddrByIndex See Program 2 4 See Also Ch GlobalSymbolTotalNum Ch_GlobalSymbolAddrByName Ch_GlobalSymbolAddrByIndex Ch_GlobalSymbolNameByIndex Ch_SymbolIndexByName 280 Chapter A APIs for Embedding Ch lt embedch h gt Ch GlobalSymbolNameB yIndex Ch GlobalSymbolNameByIndex Synopsis include lt ch h gt char Ch_GlobalSymbolNameByIndex ChInterp_t interp int num Purpose Get the symbol name of a global variable in
177. alldata ChPointer t clientdata The first argument interp is a Ch interpreter handle The second argument calldata contains the information of the current block in a running Ch program The last argument clientdata points to the data passed in the fourth argument of function Ch AddCallback Only one callback function can be specified for an interpreter A subsequent function call of Ch AddCallback will overwrite the previously specified events callback function client data and count value A callback function is invoked during the program execution Functions such as Ch RunScript and Ch ParseScript that will remove the existing Ch program shall not be called inside the callback function The callback function cannot call Ch End to terminate itself For the same reason functions such as Ch ExecScript that will execute the existing Ch program at the beginning shall not be called inside the callback function Type ChBlock t is a structure containing the information for a block used by Ch AddCallback and Ch ChangeStack typedef struct ChBlock int event event for callback int count every count instructions for CH MASKCOUNT int level int level2 levell int level level0 int leveO current func int linecurrent the current line num where the program is executing int linefuncbegin the line number where the definition of the function begi
178. also contains tag names for classes structures and unions as well as names defined by typedef The index number for the symbol table starts with 0 Symbols in header files including system header files are treated the same as in a user s pro gram Global variables qualified by declspec global are not included in the symbol table Example See Programs 2 4 and See Also Ch SymbolIndexByName Ch SymbolAddrByName Ch SymbolAddrByIndex Ch SymbolNameByIndex Ch VarType 308 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedInfo Ch UserDefinedInfo Synopsis include lt ch h gt int Ch_UserDefinedInfo ChInterp_t interp ChUserDefinedTag t udtag ChUserDefinedInfo_t udinfo Purpose Obtain the information for a user defined structure class or union type in the Ch space Return Value If the function is successul it return CH OK otherwise it returns CH ERROR Parameters interp A Ch interpreter udtag The tag for a user defined type a return value from the function Ch_UserDefinedTag udinfo A pointer to structure containing the information for a user defined type Description This function Ch_UserDefinedInfo can be used to obtain the user defined data type tag name size number of members of a user defined structure class or union or its pointer type based on its tag obtained from the function Ch_UserDefinedTag Type ChUserDefinedInfo t is a structure containing the information
179. am 5 28 Makefile to create libsample dl for Windows Makefile dl 5 5 Invoking C Classes in the Main Program from Ch Space Interface Ch with classes in C located in a library has been described in Chapter 7 Interfacing Classes and Member Functions in C of Ch SDK User s Guide Definitions of member functions for classes are compiled and linked to form dynamically loaded library with file extension dl In many applications with embedded Ch class definitions in C space invoked from Ch space are located inside the executable binary program that invokes embedded Ch programs In this case the member functions of classes in C space shall be exported so that Ch programs can callback these binary member functions Like interface C functions in an application program described in the previous sections C classes in an application can be invoked in Ch scripts with or without a dynamically loaded library in dl file The example in this section below demonstrates the mechanism for interface between Ch and C for such 99 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE callback member functions without a dynamically loaded library An example using a dynamically loaded library will be presented in next section This example is similar to the example described in section 4 of Chapter 7 in Ch SDK User s Guide Pro gram 5 29 contains the definition of class Classi with a constructor C
180. am inside the interpreter The function Ch AppendRunScript both parses and executes the code whereas function Ch AppendParseScript only parses the code to check its syntax The code is not executed Functions Ch ExprEval Ch CallFuncByAddr or Ch CallFuncByName shall be called to execute the code This implies that functions shall be appended by function Ch AppendParseScript These two appending functions return 0 on successful and 1 on failure Application of these two functions is illustrated in Program Program is modified based on Program L5 Different from Program 1 5 Program 1 7 execute two fragments of Ch code after the em bedded Ch program Program has been executed by function Ch RunScript The code fragment codel which calls function fun in Program I 6 and its own function un2 in Program I 7 is exe cuted by Ch AppendRunScript The code fragment code2 is parsed by Ch AppendParseScript first Then function Ch ExprParse is called to check if the expression fun3 30 is valid Finally function Ch ExprEval is invoked to call function un3 in the code fragment code2 The output of executing Program L 7 is shown as follows gt append exe argc 3 argv 0 embedch2 ch argv 1 opt1 argv 2 opt2 i in fun 5 i in fun 10 i in fun2 20 i in fun 100 i in fun3 30 The code that can be appended to an embedded Ch program has some restrictions For example it shall not co
181. ame in the function printUserDefined Not only the data type and address of a variable but also the tag and nested level of the user defined type are passed to function printUserDefined The argument nestlevel is used to format the indentation of the displayed output At the beginning of the function printUserDefined based on the value for nest level the format specifier format for the out put of member i y and s of class c is formed as n 14s whereas the format specifier for mem bers i and x of variable s of structure type which is also a member of the class c is n 12s The function Ch UserDefinedInfo is called to obtain the information of the user defined type The variable udinfo of the structure type ChUserDefinedInfo t contains the data type tag name size and number of members of the user defined type The member field udinfo totnum is used as the upper limit of a for loop to process all members inside the user defined type The member field udinfo size contains the size of the user defined type The return value of function printUserDefined as the size of the variable is used to handle a variable of array type A member of a user defined type itself is a variable The information about each member of of vari able of user defined type is obtained by the function ChUserDefinedMemInfoByIndex A mem ber variable in function printUserDefined is handled similar to a regular variable in function chPrintSymbolValue
182. ameters interp A Ch interpreter name Name of the variable or expression whose data type is to be obtained Description The function Ch_DataType gets the data type of a variable in Ch space within the executed program scope If a variable of array type including pointer to computational array it gives the data type of its element Unlike function Ch_FuncArgDataType For a pointer to C array it gives the pointer type of its element for the convenience of runtime manipulation of variables For example for the script code int a 3 p 2 3 array int b 3 c 2 3 The data type of a b c using function Ch DataType is CH INTTYPE The data type for p is CH INTPTRTYPE For a variable of boolean type it is treated as a char in Ch For example for the script code include lt stdbool h gt bool bo true The data type of bo using function Ch DataType is CH CHARTYPE The argument of name is a variable name in Ch space Example In this example information about global variables in the embedded Ch program varinfo ch are ob tained in the hosting C program varinfo c varinfo c A C program to get information for variables in Ch Ok kok kok kok kok kok kok kok kok kok kok A A A A kok kok kok kok kok kok kok k KC kk Kk kk kk ke ke eek File Name varinfo c CKCKCKCKCKCKCKkCKCkCkCk k k k k k k XZ k k k k k kk k k k ck k ck k ck kok ck kk k k k kk include lt stdio h gt 229
183. amically through a linked list of values or expressions obtained from the user input To build an argument list arg1ist dynamically using function Ch VarArgsAddArg data type array dimension etc may need to be obtained for the called function such as func in the hosting program such as Program 3 8 The data type array element data type and function return type of an argument of a function can be obtained by function Ch FuncArgDataType If the argument of a function is array type its array type dimension and extent for each dimension can be obtained by functions Ch FuncArgArrayDim Ch FuncArgArrayExtent and Ch FuncArgArrayType respectively Whether the argument of a function is a pointer to function pointer to function with variable number of arguments and the number of arguments of the pointer to function can be obtained by functions Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg and Ch_FuncArgFuncArgNum respectively The tag name and size of the user defined data in terms of class structure and union for an argument of a function can be obtained by functions Ch FuncArgUserDefinedName and Ch FuncArgUserDefinedSize respectively Arguments c and d for function func are computational arrays which are also called Ch arrays Either C or Ch array can be passed to an argument of Ch array type Therefore function calls Ch VarArgsAddArg interp amp arglist CH CARRAYTYPE CH INTTYPE c dim n m Ch VarArgsAd
184. ampclass h used in Program 5 38 is the same as the one used in Program 5 35 Header file Program 5 29 with the same class definition defined in Program 5 36 is used in Program 5 38 Program 5 37 runs the Ch script in Program 5 38 by the API Ch RunScript Afterward the function func in the Ch space in Program 5 38 is called by the API Ch CallFuncByName The class c1 and integer i defined inside Program 5 37 are passed to the function func The output of executable compiled from Program 5 37 is shown in Figure 5 5 108 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE File prog2 cpp include lt stdio h gt include lt embedch h gt include sampclass h int main ChInterp t interp char argvv script2 cpp NULL int status i 10 retval class Classl cl cl memfunl i initialize embedded Ch Ch Initialize amp interp NULL run a Ch program status Ch RunScript interp argvv if status printf Error execution of script2 cpp failed n Ch CallFuncByName interp func amp retval amp cl i printf retval in prog2 cpp d n retval Ch End interp return 0 Program 5 37 C program invoking embedded Ch programs prog2 cpp File script2 cpp finclude sampclass h int func class Classi c int i int j j c memfunl i printf i d j d in func in scrip
185. an environment variable or registry value PROG HOME It can be directory either c Xmy applications or usr my applications af proghome getenv PROG HOME initialize embedded Ch option shelltype CH REGULARCH len strlen proghome strlen embedch 1 option chhome char malloc len strcpy option chhome proghome strcat option chhome embedch 320 Chapter B APIs for Embedding Ch lt embedch h gt Ch VarType Ch Initialize amp interp amp option ZR E 321 Index __declspec global 45 chrc 6 path 6 8 abort array of reference 118 assumed shape array Borland 8 ch bc lib chsdk bc lib embedch bc lib make makefile C array 116 C space cl exe 6 c2chf callback CBuilder 9 Ch computational array 116 Ch space ch dll ch h CH_ABORT 205 Ch_Abort Ch AddGallback Ch AppendParseScript 242 Ch AppendParseScriptFile 201 Ch AppendRunScript Ch AppendRunScriptFile Ch_ArrayDim Ch_ArrayExtent Ch_ArrayNum Ch_ArrayType Ch CallFuncBy Addr Ch CallFuncBy Addrv 51 204 322 Ch CallFuncByName Ch CallFuncByNamev 53 Ch CallFuncByNameVar CH CARRAYPTRTYPE 260 CH CARRAYTYPE CH_CARRAYVLATYPE Ch_ChangeStack 199 Ch_ChangeStack CH CHARRAYPTRTYPE 224 260 BIA CH_CHARRAYTYPE CH_CHARRAYVLATYPE Ch ChIsFunc Ch ChIs Variable Ch ChSymb
186. and Ch End in header file embedch h located in the directory CHHOME extern include In this document these data type macros and APIs will be described in detail with examples Program l 1 is an example of C programs with embedded Ch The embedded Ch executes a Ch program named embedch1 ch with only one statement printf hello from embedchl ch n After compiling and linking file embedch1 c with a makefile such as one shown in Program 1 2 for Unix the binary executable file embedch1 exe is generated The output of executing application program embedch1 exe is as follows CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 1 GETTING STARTED BORK kok kok kk kk kk kk kk kk kk kk kk kCKCkCKCkCK kok kok kok kok kok kok kkk kkk kck I I IK File Name embedchl c executing Ch program in C program with embedded Ch with default options CKCKCKCKCkCkCKCkCKCkCkCkCK KX KA KA KA KA KA KA KA k k k kk kk k k kk k k k kk kk kk ok ko ke ke include lt embedch h gt include lt stdio h gt int main ChInterp t interp int status char argvv embedchl ch NULL initialize embedded Ch status Ch Initialize amp interp NULL if status printf Error initialization of Embedded Ch failed n run an embedded Ch program indicated by argvv status Ch RunScript interp argvv if status CH ERROR printf Error execution of program embedchl ch failedin release memory and te
187. anged For example the command 170 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER debug stopat progl ch 6 sets a breakpoint in file prog1 ch at line 6 The command debug stopat progl ch 6 i j 1 sets a breakpoint in file prog1 ch at line 6 When the breakpoint location in file progl ch at line 6 is reached the expression i j is evaluated and the breakpoint will be hit if the value for the expression i j is true The above command is the same as debug stopat progl ch 6 i j The command debug stopat progl ch 6 i j 0 sets a breakpoint in file prog1 ch at line 6 When the breakpoint location in file progl ch at line 6 is reached the expression i j is evaluated and the breakpoint will be hit if the value for the expression i j has changed On the other hand functions chClearBreakPointLine chClearBreakPointFunc and chClearBreakPointVar remove a breakpoint of line function and variable type in the linked list respectively Function chClearBreakPoints removes all breakpoints in the debugger Functions chAssign and chCall in Program 7 16 correspond to commands assign and call respectively The contents of functions chAssign and chCall are the same Inside these functions an assignment expression such as a 2 10 or function call expression such as func is parsed by the API Ch_ExprParse to check any syntax error If the expression is valid it is evaluated by the API Ch_Expr
188. apter A APIs for Embedding Ch lt embedch h gt Ch FuncArgArrayType Ch FuncArgArrayType Synopsis include lt ch h gt ChType t Ch_FuncArgArrayType ChInterp_t interp const char funcname int argnum Purpose Determine if an argument of a function in the Ch space is array and its array type Return Value Based on its argument this function returns one of the macros below defined in header file ch h Macro Description Example CH_UNDEFINETYPE not an array int i CH_CARRAYTYPE C array int a 3 CH_CARRAYPTRTYPE pointer to C array int ap 3 CH_CARRAYVLATYPE C VLA array int a n int func int a n int b int c amp CH CHARRAYTYPE Ch array array int a 3 CH CHARRAYPTRTYPE pointerto Ch array array int ap 3 CH CHARRAYVLATYPE Ch VLA array array int a n int fun array int a n array int b array int c amp Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function 0 for the first argument Description The function Ch_FuncArgArrayType determines if the argument of a function is array type The data type of the array for an argument of a function shall be determined by function Ch FuncArgDataType Unlike function Ch_ArrayType a poiner to C array is treated as an array by function Ch FuncArgArrayType so that the complete information for an argument can be obtained Example See Ch_FuncArgDataType
189. ar interp b2 CH CHARRAYTYPE CH INTTYPE b 1 m Ch SetVar interp sp CH STRUCTPTRTYPE amp s Ch SetVar interp s CH STRUCTTYPE tag amp s Ch SetVar interp c CH CLASSTYPE tagc amp c Ch SetVar interp cp CH CLASSPTRTYPE amp c Ch SetVar interp u CH UNIONTYPE tagu amp u Ch SetVar interp up CH UNIONPTRTYPE amp u Ch SetVar interp sa CH STRUCTPTRTYPE sa Ch SetVar interp fp CH PROCPTRTYPE NULL Ch SetVar interp fp2 CH PROCPTRTYPE Ch SymbolAddrByName interp Ch SetVar interp stream CH FILEPTRTYPE FILE Ch SymbolAddrByName interp stdout Ch CallFuncByName interp callback amp retval Ch End interp setvar ch A Ch program with variables sharing data in C space include lt array h gt include lt stdio h gt struct tag Int 1j double f class tage public int i double l union tagu rnt i double l int i double dp char c int a array int a2 2 3 struct np 13 tag s class tagc c union tagu u struct int fp 0 rInt C tp2y 7 FILE s tream int func prin return 0 t func b sp cp up tag sa int callback b2 3 called n 297 Ch_SetVar func Chapter A APIs for Embedding Ch lt embedch h gt printf dp Sf n dp printf chp s n chp printf a 1 1 d n a 1 1 printf b 1 d n b 11
190. are linked with compiled objects to create a binary executable application program The sample Makfile file in CHHOME toolkit demos embedch chapters chapterl Makefilecontains options for com pilation and linking application programs with Embedded Ch for specific platforms Users can still use a graphical Integrated Development Environment to develop applications with Embedded Ch Additional information about how to setup paths for header files and libraries for different platforms for portable com pilation and linking using Ch SDK can be found in Chapter 1 in Ch SDK User s Guide 1 2 Building Executables in Windows Executables with Embedded Ch in Windows can be developed using different compilers either in command shell or in an Integrated Development Environment IDE 1 21 Building Executables in Windows Using Command Shell The makefile shown in Program L 3 can be used to build all executables in Ch command shell for Windows for all examples described in this document Assume C Ch is the home directory for Ch the header files ch h and embedch h are located in the directory C Ch extern include which should be added to the search path for header files in compilation By default the option MD shall be added in compilation for using the multi thread dynamically linked system libraries The libraries chsdk lib and embedch lib 4 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 2 BUILDING EXECUTABLES IN WINDOWS
191. ared A breakpoint can be setup based on three specifications file name and line number function and controlling variable When a breakpoint is setup in a function the program will stop at its first executable line of the function When a breakpoint is setup for a variable the program will stop when the value of the variable changes Each breakpoint can have an optional conditional expression When a breakpoint location is reached the conditional expression is evaluated if it exists The breakpoint is hit only if the expression is either true or has changed which needs to be specified when the breakpoint was added In this implementation by default the breakpoint is hit only if the expression is true If this sample debugger is modified as a GUI based debugger the above debugging capabilities could be implemented using clickable buttons or type in commands in an interactive input output window in the user interface The help command displays the debug menu shown in Figure 7 10 The variables expressions and functions can be manipulated by the last three commands in Figure 7 10 The command assign assigns a value to a variable ca11 invokes a function and print prints out the value of a variable or expression including functions It is invalid to print an expresion of void type including a function with return type void One can also just type an expression the value of the expression will be displayed If the expression is a function with the re
192. arg 2 of f5 is dWMn Ch FuncArgArrayDim interp f5 1 printf dim of arg 3 of f5 is dWMn Ch FuncArgArrayDim interp f5 2 printf dim of arg 4 of 5 is dWMn Ch FuncArgArrayDim interp f5 3 printf dim of arg 5 of f5 is dWMn Ch FuncArgArrayDim interp f5 4 printf dim of arg 6 of f5 is dWMn Ch FuncArgArrayDim interp f5 5 printf dim of arg 7 of f6 is dWMn Ch FuncArgArrayDim interp f6 6 printf dim of arg 1 of f8 is dWMn Ch FuncArgArrayDim interp f8 0 printf dim of arg 2 of f8 is dWMn Ch FuncArgArrayDim interp f8 1 printf dim of arg 3 of f8 is dWMn Ch FuncArgArrayDim interp f8 2 printf Extent of arg 1 of fl is dWMn Ch FuncArgArrayExtent interp f1 0 0 printf Extent of arg 4 of fl is dWMn Ch FuncArgArrayExtent interp f1 3 0 printf Extent of arg 1 of f2 is dWMn Ch FuncArgArrayExtent interp f2 0 0 printf Extent 1 of arg 2 of f4 is d n Ch FuncArgArrayExtent interp f4 1 0 printf Extent 2 of arg 2 of f4 is d n Ch FuncArgArrayExtent interp f4 1 1 printf Extent of arg 3 of f4 is dWMn Ch FuncArgArrayExtent interp f4 2 0 printf Extent of arg 4 of f4 is dWMn Ch FuncArgArrayExtent interp f4 3 0 printf Extent of arg 5 of f4 is dWMn Ch FuncArgArrayExtent interp f4 4 0 printf Extent 1 of arg 2 of f5 is d n Ch FuncArgArrayExtent interp f5 1 0 printf Extent 2 of arg 2 of f5 is d n Ch FuncArgArr
193. arger than 5120 bytes Ch AppendRunScript saves it in a temporary file in ternally first then calls the API Ch AppendRunScriptFile to execute the file and finally removes the tempoary file is removed Alternatively if the code is larger than 5120 bytes it can be broken into multiple segments The user can then call the function multiple times to load these segments of code as shown below Ch AppendRunScript interp codel Ch AppendRunScript interp code2 218 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendRunScript This allows the code to run from the memory and is more efficient Example Refer to Program 1 7 See Also Ch AppendParseScript Ch RunScript Ch ParseScript Ch ExecScript 219 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendRunScriptFile Ch AppendRunScriptFile Synopsis include lt embedch h gt int Ch AppendRunScriptFile ChInterp t interp const char filename Purpose Append a fragment of the Ch code in a file to an existing Ch program inside the interpreter executed by functions Ch RunScript or Ch ParseScript and Ch ExecScript The Ch code will be parsed and executed Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter filename File name that contains the Ch code to be appended to the existing Ch program inside the inter preter Description When a program is run in Ch it first wi
194. ass with constructor the constructor of a global variable of class type is executed at runtime In some applications it may be desirable to change this default behavior by calling this function using Ch_InitGlobalVar interp 1 before a program is parsed by functions Ch ParseScript Ch_RunScript Ch_AppendParseScript and Ch_AppendRunScript The initialization of a global variable or execution of the constructor of a variable of class type is then performed at the parse time If the same interpreter is used to handle different scripts this function can be called to change the default behavior before a script is parsed Example In this example the script initializes the global variable g and declares variable c of class with a constructor Function Ch InitGlobalVar is called in case 3 to enable the execution of the con structor at the parse time Therefore the proper execution sequence is maintained The constructor is executed first Then the member function is executed Finally the destructor is executed initglobalvar c A C program to run initglobalvar ch include lt embedch h gt include lt stdio h gt int main 283 Chapter A APIs for Embedding Ch lt embedch h gt Q hInterp t interp har argvv initglobalvar ch NULL Q rintf case 1 Ch InitGlobalVar interp 0 n h_Initialize amp interp NULL h_ParseScript interp argvv CallFuncByName interp func NULL ExprEv
195. ataType interp expr switch datatype case CH_INTTYPE retval Ch_ExprCalc interp expr datatype printf s d n expr i break case CH_DOUBLETYPE retval Ch_ExprCalc interp expr datatype printf s f n expr d break default printf datatype d not supported datatype Ch Home interp break return retval data void int data double data add it based on s extern include ch h n const char expr amp 1 sa add it based on s extern include ch h n Program 7 11 C program for obtaining information for variables of user defined type prog5 c Contd 163 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES include lt stdio h gt int i 40 double g 100 double r 200 void funcl int i double g 200 int y printf funcl is called n Ei A int i 20 z i 30 printf argument i in funcl is true n printf i in funcl is d n i printf y in funcl is dWn y printf g in funcl is f n g int main int i 10 x ftuneltiir printf x in main is d n x printf g in main is f n g printf r in main is f n r return 0 Program 7 12 Ch program with user defined data type prog5 ch 164 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM funcl is called argument i in funcl event MASKE
196. atement addr char addr size This updated address is then passed to the function printValue A one dimensional array of char is treated as a string Characters after a terminating null character VO for a string in an array of char are not printed out For example if the array s declared with 10 elments was filled with the string abc only the first 3 elements of the array will be printed out If the data type of a variable is structure class or union function printUserDefined is called to print out the value for each member of the user defined type inside function printValue Member names and their values of a variable of user defined type are printed out with a proper indentation A member of a user defined type itself might be also a user defined type In this case its names and values are further indented For example the values for variables k and c in the code below int k 5 struct tagl rint dy X class tag2 int i y struct tag s c 10 20 30 40 will be displayed by the function printUserDefined as follows 154 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES k 5 e i 10 y 10 S i 30 40 The name for variable k of int type is printed out by the statement printf 16s name near the end of function chPrintSymbolValue Member names of a variable are printed out by the statement printf format meminfo memn
197. ayExtent interp f5 1 1 printf Extent of arg 3 of f5 is dWMn Ch FuncArgArrayExtent interp f5 2 0 printf Extent of arg 4 of f5 is dWMn Ch FuncArgArrayExtent interp f5 3 0 printf Extent of arg 5 of f5 is dWMn Ch FuncArgArrayExtent interp f5 4 0 printf Extent of arg 6 of f5 is dWMn Ch FuncArgArrayExtent interp f5 5 0 printf Extent of arg 7 of f6 is dWMn Ch FuncArgArrayExtent interp f6 6 0 printf Extent 1 of arg 1 of f8 is d n Ch FuncArgArrayExtent interp f8 0 0 printf Extent 2 of arg 1 of f8 is d n Ch FuncArgArrayExtent interp f8 0 1 printf Extent 1 of arg 2 of f8 is d n Ch FuncArgArrayExtent interp f8 1 0 printf Extent 2 of arg 2 of f8 is d n Ch FuncArgArrayExtent interp f8 1 1 printf Extent 1 of arg 3 of f8 is d n Ch FuncArgArrayExtent interp f8 2 0 printf Extent 2 of arg 3 of f8 is d n Ch FuncArgArrayExtent interp f8 2 1 printf arg 1 of fl is function d n Ch FuncArgIsFunc interp f1 0 printf arg 4 of fl is function d n Ch FuncArgIsFunc interp f1 3 printf arg 1 of f7 is function d n Ch FuncArgIsFunc interp f7 0 printf arg 2 of f7 is function d n Ch FuncArgIsFunc interp f7 1 printf arg 3 of f7 is function d n Ch FuncArgIsFunc interp f7 2 printf arg 4 of f7 is function d n Ch FuncArgIsFunc interp f7 3 printf arg 1 of f8 is function d n Ch FuncArgIsFunc interp f8
198. back is triggered by the call event for the function funcl the argument i has the value 10 passed from the function main Entering the if statement block before its initialization the local variable i s is 0 Before the end of the if statement block the value for the local variable i is 30 The value for the argument i before leaving the function unc1 is 10 Similarly the value for the local variable i inside function main is 10 as shown in Figure 7 1 7 2 Changing Stack The information for variables within its scope can be obtained by various APIs inside a callback function For example variable i declared in an if statement block in Program 7 1 can be accessed using the API Ch SymbolAddrByName inside function callback when it is triggered by either beginning block or end block event The API Ch_SymbolAddrByName can also access variables inside a function within its scope For example when the callback function is triggered by the end block event inside the if statement in function func1 in Program 7 1 local variables y and g inside function unc1 are accessible by the Ch SymbolAddrByName in function callback The argument i of function funcl is also accessible by th index of variables in its symbol table even though the identifier i is used for both local variables and argument The global variables i g and a in Program 7 1 however can be accessed by the API Ch GlobalSymbolAddrByName even when the act
199. bedch4 ch invoked n return 0 void chfun double ch_d int ch_data printf in the Ch function chfun ch d f ch data 0 d ch_data 9 d n ch d ch data 0 ch data 9 ch d 1 ch data 0 ch data 9 Program 4 2 Example of sharing data in C and Ch spaces embedch4 ch embedch4 ch invoked in embedch4 c c d 10 000000 c data 0 1 c data 9 10 in the Ch function chfun ch d 10 000000 ch data 0 1 ch data 9 10 in embedch4 c c d 11 000000 c data 0 2 c data 9 11 in the Ch function chfun ch d 100 000000 ch data 0 101 ch data 9 110 in embedch4 c cd 101 000000 c data 0 102 c data 9 111 Figure 4 1 Output from executing embedch4 c 4 2 Accessing Variables in a Header File in C Space from Ch Space Accessing global variables in C space in a static or dynamically loaded library declared in a header file from Ch space has been described in Chapter 4 in Ch SDK User s Guide The methods are simple and readily applicable to embedded Ch programs In this section a same example presented there will be used to illustrate how to access global variables in a main application program in C space by embedded Ch scripts Program 4 3 is a C header file containing two global variables i1 and i2 We assume the variable il remains unchanged after the original value is assigned whereas variable i2 will be changed during the execution of the C program These
200. bedded Ch program has multiple functions but without function main as shown below embedded Ch program header files int funcl S ae NY void func2 1 Z 11 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE KKK KK A X e X e x e x File Name 1 3 OTHER ESSENTIAL APIS AND OPTIONS FOR EMBEDDED CH Ck ck ck ck ck ck ck ck ck ck ck ck ck ck ck AX kk X AX kk ck kk kk KK KH A KK Sk Sk A A A A kx kx ko kx embedch2 c executing Ch program in C program with embedded Ch with user de KKKKKKKKKKKKKKK include lt stdio fined options TK kok kok kok kok kok kok kok kok kok kok k kk kk kk kk k k ck kk kk kk kk k kk kk kok n h h h include lt string include lt stdlib include lt embedch int main ChInterp t in int status char argvv embedch2 ch ChOptions t option int retval terp optl opt2 NULL initialize embedded Ch option shelltype CH REGULARCH option chhome strdup embedch Ch Initialize amp interp amp option Embedded Ch Home run an Embedded Ch program indicated by argvv status Ch RunScript interp argvv if status CH ERROR printf Error execution of program embedch2 ch failed n Ch CallFuncByName interp fun amp retval 10 release memory and terminate the shell free option chhome Ch End interp Program 1 5 A C program with embedded Ch
201. been changed from EMBEDCH HOMB bin chmt dl to EMBEDCH_HOME extern lib chmt d1 so that they are located in the same location for all platforms For multiple instances of Em bedded Ch engine copy EMBEDCH_HOME extern lib chmt1 dl to EMBEDCH HOME extern lib chmt2 dl etc B 2 Porting Code with Embedded Ch APIs to Ch Version 5 5 To simplify the distribution of Embedded Ch the member fields chre chrcname chmtdir of struct ChOpt oin_t are deprecated if the user specifies the home directory of the Embedded Ch in the application The file option chhome config chrc is used as the system startup file The directory option chhome bin in Windows and option chhome extern lib in other platforms shall contain dynamically loaded libs chmt1 dl chmt2 dl etc If the user does not specify the home directory of Embedded Ch the startup file CHHOME config chrc is used the dynamically loaded libs chmt1 dl chmt2 dl etc are located in in C Windows System32 for Windows and usr lib for Unix we assume that you distribute CHHOME toolkit embedch along with your application It is distributed in your application home sub directory such as c Mmny applications Nembedch or usr my applications embedch finclude lt embedch h gt include lt stdio h gt include lt string h gt include lt stdlib h gt int main int status len ChInterp t interp ChOptions t option char proghome get the home dir for your application program from
202. ber field option chhome for the embedded Ch home directory can be set to the current working directory In Unix no additional file is needed 28 Chapter 2 Accessing Ch Variables from C Space In the previous chapter we introduced the basic procedure and core APIs for embedding Ch After a Ch program is executed by a C C program users may want to access and modify the value of variables or evaluate expressions in the Ch program We will describe APIs which can be used to access variables assign values of variables and evaluate expressions in the Ch space from a C C program in the C space 2 1 Accessing Variables by Addresses The basic method to access a variable in Ch program typically consists of two steps 1 Getting the address of a variable in Ch space within its scope by API Ch SymbolAddrByName with its function prototype in header file ch h as follows void Ch SymbolAddrByName ChInterp t interp char name The first argument of function Ch SymbolAddrByName is an instance of Ch interpreter The second argument is the name of the symbol whose address in Ch space is obtained The function returns the address obtained on successful execution or a NULL pointer on failure Function Ch SymbolAddrByName can obtain the address of both local and global variables Function Ch GlobalSymbolAddrByName can be used to obtain the address of a global variable when the interpreter is executing a function as shown in an example
203. break case CH STRUCTTYPE case CH CLASSTYPE case CH UNIONTYPE size printUserDefined interp datatype addr udtag nestlevel break case CH CHARPTRTYPE if char addr printf s char addr else printf NULL size sizeof char break case CH_VOIDPTRTYP case CH_INTPTRTYPE case CH_FLOATPTRTYPE case CH_DOUBLEPTRTYPE case CH_STRUCTPTRTYPE case CH_CLASSPTRTYPE case CH_UNIONPTRTYPE printf OX p void addr E size sizeof void break case CH_UNDEFINETYPE invalid expr printf invalid expression break default printf datatype d not supported in printValue in file stack c add it based on s extern include ch h datatype Ch Home interp break return size 153 Program 7 9 Functions for printing values of all variables in a stack stack c Contd CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES such as dlclose math will not be displayed This might be desirable to minimize the displayed symbols for clarity In Program 7 9 this flag is set 1 so that all these variables will be displayed Function Ch ArrayType will return a non zero value if the variable is of array type If the variable is not of array type the value of the variable is printed out by the function
204. c char argv ChInterp t interp int status int num retval char argvv embedchl ch NULL Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error Ch RunScript failed n exit 1 num sizeof data sizeof double printf calling the printdata in Ch n status Ch_CallFuncByName interp printdata amp retval num data printf retval d n retval printf after calling the printdata in Ch n if status CH ERROR printf Error Ch CallFuncByName failed n exit 1 Ch End interp int printdata int num double data int i for i 0 i num i printf data d Sf n i data i return 100 this function is called from the Ch space EXPORTCH int printdata_chdl void varg ChInterp t interp ChVaList t ap int num retval double data Ch VaStart interp ap varg num Ch VaArg interp ap int data Ch VaArg interp ap double printf data in printdata in C is retval printdata num data Ch VaEnd interp ap return retval Sp n data Program 5 15 A C program calling back a C function from a Ch function prog1 c 89 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File embedchl ch include lt stdio h gt include sample h printdata can be pla
205. cArgUserDefin printf data name for arg 6 of f6 is sWMn Ch FuncArgUserDefin printf data name for arg 7 of f6 is s n Ch FuncArgUserDefin printf data name for arg 8 of f6 is s n Ch FuncArgUserDefin printf data name for arg 9 of f6 is s n Ch FuncArgUserDefin Ch End interp funcarg ch A Ch program with global variables include lt array h gt struct tag int i double f un class tage public Ine d double f un union tagu int 17 double f V int fl int a int 2 void f3 int a double dp char cp void f4 int n int a1 2 3 int a2 n n int a3 int a4 amp void f5 int n array int al 2 3 array int a2 n n array int a3 array int a4 amp array int amp a void f6 struct tag s struct tag sp class tagc c class tagc cp union tagu u union tagu up 265 Ch FuncArgDataType rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp rp TETE fg WERU 9 Chapter A APIs for Embedding Ch lt embedch h gt struct tag sa 4 struct tag f1 int i struct tag f2 int i void f7 int f1 int int f2 int 3 int double char int f4 int void f8 array double f1 int 2 3 array double f2 int array double f3 int amp amp Output arg 1 of fl is int arg 2 of fl is undefined arg 1 of f2 is undefined arg 2 of f3 is double ar
206. ced in function file printdata chf int printdata int num double data void fptr int retval fptr dlsym _Chsample_handle printdata chdl if fptr NULL fprintf stderr Error s dlsym s n X func dlerror return 1 printf data in printdata in Ch is p n data dlrunfun fptr amp retval printdata num data return retval the function main is optional int main double data 4 1 0 2 0 3 0 4 0 printdata 4 data return 0 Program 5 16 Ch program embedch1 ch this header file will initialize Chsample handle ifndef sample H define sample H ifdef CH include lt dlfcn h gt void _Chsample_handle ifndef _HPUX_ _Chsample_handle dlopen NULL RTLD LAZY if Chsample handle NULL fprintf stderr Error dlopen s n dlerror fprintf stderr cannot get Chsample handle in sample h n exit 1 void _dlclose_sample void dlclose Chsample handle atexit dlclose sample fendif fendif extern int printdata int num double data fendif Program 5 17 C Ch header file sample h 90 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE calling the printdata in Ch data in printdata in Ch is 26960 data in printdata in C is 26960 data 0 10 000000 data 1 20 000000 data 2 30 000000 data 3 40 000000 retval
207. ck ck ck ck ck ck kok kk kok ke ke ke include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status double result char expr 512 strcpy expr 2 1 pow 2 0 3 cos 0 3 Ch Initialize amp interp NULL initialize embedded Ch status Ch ExprParse interp expr if status CH ERROR printf s is invalid n expr else Ch_ExprCalc interp expr CH_DOUBLETYPE amp result printf s f n expr result Ch End interp Program 2 12 Evaluating an expression in the Ch space exprcalc c 2 I pow 2 0 3 cos 0 3 18 955336 The expression evaluated by functions Ch ExprEval and Ch ExprCalc may contain functions lo cated in function files If the Embedded Ch is used to evaluate an expression which may contain func tions no Ch program needs to be loaded explicitly by functions such as Ch RunScript as shown in Pro gram 2 12 Variables and functions in the Ch space can also be added by APIs Ch AppendParseScript and Ch AppendRunScript without calling Ch RunScript first then used by APIs Ch ExprParse Ch ExprEvalO and Ch ExprCalc described in section 3 4 2 5 Removing and Redeclaring Variables Variables in the Ch space can declared inside a Ch program loaded by Ch RunScript Ch RunScriptM and Ch ParseScript After the program is loaded and executed additional global variables can be ad
208. ck is initialized with the callback function The member clientdata is initial ized to the debugger debug itself so that it will be passed as client data to the callback function These members will be used as arguments of the API Ch AddCallback to register the callback function Like a GUI program this sample debug program is also event driven After initialization the program waits for the user input from the function chProcessActionMenu After a script is processed the program is terminated by function chEndDebug Function chCleanDebug cleans the allocated memory Note that the API Ch End may invoke some functions registered by the standard C function atexit in the Ch space in a header file When these registered functions are called the callback function will be triggered 168 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER which will use the linked list of breakpoints in the debugger Therefore the function Ch End must be called first before chC leanDebug is called which in turn calls chClearWatchExprs to clear all expressions in the watched list and chClearBreakPoints to clear all breakpoints One may change the function main as a regular function so that it can be called to debug different programs Before we explain other functions we will take a look at how the user interface function chProcessActionMenu is implemented in Program 7 16 The while loop inside function chP
209. class h Program 5 40 for class definition file sampclass cpp is compiled and linked with Program 5 32 for pro gram prog cpp to form an executable binary program The process for compiling and linking of pro grams can be automated by a Makefile program Program S 41 can be used to create executable program prog exe used in this example for Unix The similar makefile in Programs 5 22 can be used for Windows 110 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE ifndef SAMPCLASS H define SAMPCLASS H KR KKK KR KK for Ch space OK KK x Ke ifdef CH include lt dlfcn h gt extern void g sample dlhandle dlopen libsampclass dl RTLD LAZY if g sample dlhandle NULL fprintf stderr Error dlopen s n dlerror fprintf stderr cannot get g sample dlhandle in sampclass h Nn exit 1 void dlclose sampclass void dlclose g sample dlhandle atexit dlclose sampclass fendif class Classl fifndef CH for C space KKK sk ke e x x x x private int mil public EXPORTCHCLASS Class1 EXPORTCHCLASS Class1 EXPORTCHCLASS int memfunl int i else public Classl Classl int memfunl int i fendif pragma importf sampclass chf endif SAMPCLASS H Program 5 39 Ch C header file sampclass h 111 CHAPTER 5 CALLING C C FUNCTIONS FROM CH
210. ction Ch Initialize with the data type of its first argument of a pointer to ChInterp_t The second NULL in the argument list indicates default options User defined options of Ch shell will be described later Function call CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 1 GETTING STARTED fassume CHHOME is usr local ch If not change usr local ch to CHHOME such as usr ch CC cc INC I usr local ch extern include CFLAG LFLAG usr local ch extern lib libchsdk a usr local ch extern lib libembedch a 1m lsocket ldl target embedchl ex mbedchlb ex mbedch2 exe reopen exe mbedch exe chhome ex mbedchprograms exe parseprograms exe thread exe threads exe append ex mbedch exe embedch c CC embedch c INC S LFLAG CFLAG o embedch ex mbedchl exe embedchl c CC embedchl c INC LFLAG CFLAG o embedchl ex mbedchlb exe embedchlb c CC embedchlb c INC S LFLAG CFLAG o embedchlb ex mbedch2 exe embedch2 c CC embedch2 c S INC S LFLAG CFLAG o embedch2 ex reopen exe reopen c CC reopen c INC LFLAG CFLAG o reopen exe chhome exe chhome c CC chhome c INC S LFLAG CFLAG o chhome exe embedchprograms exe embedchprograms c CC embedchprograms c INC LFLAG CFLAG o embedchprograms exe parseprograms exe parseprograms c CC parseprograms c INC S LFLAG S CFLAG o parseprograms exe thread exe thread c CC thread c INC LFLAG
211. ction prototype without function definition or an extern variable with out definition the function returns NULL variable of C array Ch computational array or pointer to Ch computational array the returned value is the address of the first element For a variable of pointer to C array the returned value is the address of the pointer to C array i e the address of the first element For example for variables of a and p declared in Ch int pa 3 p al2 3 the returned values for both pa and p are the same of pointer to pointer to int The return value for a is the address of the first element a 0 0 The data type for variable a returned from Ch DataType is CH INTTYPE whereas the data type for variables pa and p returned from Ch DataType is the same as CH INTPTRTYPE Note that in C the address of function is the same as the function For example the output from the two printing statements below are the same int func printf address of func p n func printf address of func p n amp func Parameters interp A Ch interpreter name The name of a variable in a Ch program Description The function Ch SymbolAddrByName obtains the address of a variable within its most inner scope of a Ch program For example it will first search for the variable in the block scopes then in the function scope and finally in the program scope The variable can be valid data type in Ch The value of a variable in
212. cture etc By assigning the address of a variable or data in the C space to a variable of pointer type in the Ch space the memory in the C space can be shared by the variables in the Ch space For example Program 2 5 declares three variables ch i of int ch p of pointer to int and ch data of pointer to int in the Ch space by function Ch AppendRunScript The value of the variable c i in the C space and its address are assigned to variables ch i and ch p respectively by function calls 33 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 2 ASSIGNING VALUES IN C SPACE TO VARIABLES IN CH SPACE File Name setvar c include lt stdio h gt include lt embedch h gt int main ChInterp t interp int status int c i c data 10 1 2 3 4 5 6 7 8 9 10 initialize embedded Ch Ch Initialize amp interp NULL declare variables i p ch data in Ch space Ch AppendRunScript interp int ch i ch p ch data c i 10 assign c i in C space to ch i in Ch space Ch SetVar interp ch i CH INTTYPE c i Ch ExprEval interp printf W ch i d n ch_i assign amp c i in C space to ch p in Ch space Ch SetVar interp ch p CH INTPTRTYPE amp c i c i 20 Ch ExprEval interp printf ch_i d n ch_i Ch_ExprEval interp printf ch_p d n ch_p Ch_ExprEval interp ch_p 30 printf c i d n
213. cvptr CH FUNCPTRTYP printf funcvptr is a pointer to function n printf Number of arguments of function funcvptr Ch FuncArgNum interp funcvptr 1 if Ch_IsFuncVarArg interp printf funcvptr funcvptr if Ch DataType interp s CH STRUCTTYPE udtag Ch UserDefinedTag interp s Ch UserDefinedInfo interp udtag amp udinfo printf udinfo dtype CH STRUCITYPE d n udinfo dtype CH STRUCTTYPE printf struct size of s is d n udinfo size printf struct name of s is s n udinfo tagname printf the number of members of s is dMn Ch UserDefinedMemInfoByName interp udtag f printf meminfo index d n meminfo index printf meminfo memname s n meminfo memname printf meminfo offset d n meminfo offset printf meminfo dtype CH DOUBLETYPE d n meminfo dtype CH DOUBLETYPE printf meminfo ispublic d n meminfo ispublic printf meminfo isfunc d n meminfo isfunc sp Ch_SymbolAddrByName interp s printf sp gt i dMn sp gt i printf sp gt f f n sp gt f printf Ch_DataSize interp s d n Ch DataS if Ch_DataType interp sp CH STRUCTPTRTYPE udtag Ch_UserDefinedTag interp sp Ch UserDefinedInfo interp udtag amp udinfo printf struct size of sp is dWMn udinfo size printf struct name of sp is s n sp2 Ch SymbolAd
214. d Ch Standard Edition Embedded Ch Stan dard Edition contains all features of regular Ch Standard Edition except that it cannot be started standalone All capabilities in Ch Standard Edition can be invoked from your application program with Ch embedded However the size of Embedded Ch Standard Edition can be significantly reduced by removing the docu mentations demo programs Unix utilities in Windows and toolkits such as OpenGL Win32 GTK The size of such a slim Embedded Ch Standard Edition with all ISO C standard libraries is less than 3 Mb Applications developed using Embedded Ch Evaluation Edition can only run in a machine where Em bedded Ch Evaluation Edition is installed This documentation contains detailed information about the use of Embedded Ch SDK For information about Ch and Ch SDK please check the corresponding documentation Typographical Conventions The following list defines and illustrates typographical conventions used as visual cues for specific elements of the text throughout this document e Interface components are window titles button and icon names menu names and selections and other options that appear on the monitor screen or display They are presented in boldface A sequence of pointing and clicking with the mouse is presented by a sequence of boldface words Example Click OK Example The sequence Start gt Programs Ch6 1 Ch indicates that you first select Start Then select submenu Programs b
215. d argument of function call ChInitialize should not be NULL such as 25 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 7 DISTRIBUTING EMBEDDED CH IN YOUR APPLICATIONS We assume that your application is distributed in usr local companyname in Unix or C myApplication in Windows The Embedded Ch runtime environment should be copied from CHHOME toolkit embedch in your development machine and distributed at usr local companyname embedch in Unix and C myApplication embedch in Windows in a target machine Then Embedded Ch home directory will be usr local companyname embedch in Unix and C myApplication embedch in Windows include lt embedch h gt include lt stdio h gt include lt string h gt include lt stdlib h gt int main int status len Chinterp_t interp ChOptions t option char proghome p get the home dir for your application program from an environment variable or registry value PROG HOME which was setup during the installation of your application in a target machine The Embedded Ch home directory will be PROG HOME embedch which needs be setup using option chhome as follows set option shelltype option shelltype CH REGULARCH set option chhome proghome getenv PROG HOME from an environment variable or obtain the va lue for proghome from a registry value in Windows using Windows API RegOpenKeyEx and RegQueryValue
216. d conflict in general the dynamically linked library ch d11 for Embedded Ch shall be located at the same directory which contains the hosting application Description The function Ch Initialize initializes embedded Ch for executing Ch programs from the C space The Ch shell type and the startup file to be used are indicated in the argument option This function should be called before the API Ch End If the argument op any Ch program is invoked The embedded Ch initialized will be ended by tion is NULL the default value for ChOptions is used It is assumed the value for the field sen1 Lltype is CH REGULARCH the field value for chhome is the home di rectory CHHOME of the standalone Ch installed in the system The startup file in CHHOME config chrc will be executed The Embedded Ch in the application searches for the dynamically loaded libraries such as chmt1 d1 and chmt2 d1 in the Windows system direc tory such as C Windows System32 for Windows and in the directory usr 1ib for other platforms 286 Chapter A APIs for Embedding Ch lt embedch h gt Ch Initialize If the argument opt ion is not NULL the startup file option chhome config chrc will be exe cuted The Embedded Ch in the application searches for the dynamically loaded libraries such as chmtl dl and chmt2 dl in the directory C myApplication embedch bin for Win dows and in the directory dir for myApplication embedch extern lib for othe
217. dArg interp amp arglist CH CARRAYTYPE CH INTTYPE d dim n m for building the argument list of the second call of API Ch CallFuncByNameVar in Program 3 8 can be replaced by Ch VarArgsAddArg interp amp arglist CH CHARRAYTYPE CH INTTYPE c dim n m Ch VarArgsAddArg interp amp arglist CH CHARRAYTYPE CH INTTYPE d dim n m Note that Program does not contain function main The code is processed first by API Ch RunScript as if it was an initialization for a Ch script This kind of initialization can be useful for handling a large script code with many functions which are invoked later in the host program For example header file windows h which contains a large number of lines of source code can be first processed in the API Ch RunScript Then Windows API in windows hcancan be called by Ch functions from the host program 3 6 Calling Ch Functions with Variable Number of Arguments Calling Ch functions with a variable number of arguments will be described in this section In all previous examples Ch functions take no argument or fixed number of arguments In practice some Ch functions can take variable number of arguments for example int funcl va list ap int func2 int ip ssal int funca re 60 e CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS These three typical prototypes can represents almost all Ch f
218. dded Ch If the value of field chhome is NULL the Ch home directory for the regular Standard or Professional Edition is used for the Embedded Ch In most applications the Embedded Ch home directory is different from that for a regular Ch In this example the Embedded Ch home directory is specified as embedch The startup file config chrc in the Embedded Ch home directory will be used More information about Ch startup file and safe Ch shell is available in Ch User s Guide The values for arguments argc and argv of function main inside an embedded Ch program embedch2 ch shown in Program I 6are obtained from the second argument of function Ch RunScript Ch RunScript interp argvv is similar to invoke program embedch2 ch in a command shell as follows embedch2 ch optl opt2 The output of executing file embedch2 exe is shown as follows gt embedch2 exe argc 3 argv 0 embedch2 ch argv 1 optl argv 2 opt2 1 in fun 5 i in fun 10 gt The Ch program embedch2 ch is executed to generate the above output when the function Ch RunScript is called At that point the Embedded Ch maintains the states of the interpreted program Functions such as Ch CallFuncByName Ch CallFuncByNameVar Ch CallFuncByAddr a Ch ExprCalc and Ch ExprEval can be used to access the states In Program I 5 Ch CallFuncByName is used to call function fun in the embedded program embedch2 ch explicitly If an em
219. ddrByName returns NULL Example See Ch_DataType See Also Ch_DataType Ch ArrayDim Ch_ArrayExtent Ch_ArrayType Ch_FuncType Ch_FuncArgNum Ch_UserDefinedTag Ch_UserDefinedInfo Ch_VarType 288 Chapter A APIs for Embedding Ch lt embedch h gt Ch ParseScript Ch ParseScript Synopsis include lt embedch h gt int Ch ParseScript ChInterp t interp const char argv Purpose Parse a Ch program Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp Ch interpreter argv File name and arguments of the Ch program The last field must be NULL Description When a program is run in Ch it first will be parsed then executed Unlike function Ch RunScript which performs these two steps together The function Ch ParseScript parses a Ch program only Later the parsed program will be executed through function Ch ExecScript or Ch ExecScriptM The variable argv contains the file name and arguments of the Ch program to be executed It must end with NULL in the last element of the variable The API Ch Initialize should be called before this function is called Example Refer to Program 1 4 See Also Ch AppendParseScript Ch AppendRunScript Ch ExecScript Ch ExecScriptM Ch RunScript Ch RunScriptM Ch Initialize Ch Home Ch_SymbolAddrByName Ch DataType Ch ExprEval Ch End 289 Chapter A APIs for Embedding Ch
220. de below needs to be added to the startup file chrc in the user s home directory path stradd path C Program Files Intel Compiler C 10 1 021 1A32 Bin putenv stradd LIB C Program Files Intel Compiler C 10 1 021 1A32 1ib getenv LIB putenv stradd INCLUDE C Program Files Intel Compiler Ct 10 1 021 IA32 include getenv INCLUDE You also need to use the Intel C compiler and linker command icl exe by changing CC cl to CC icl in the distributed make files such as Makefile 1 23 5 Building Executables in Windows Using Borland C Compiler Executable binary programs and dynamic loaded libraries in Windows can also be built using Borland C compiler Examples using Borland C compiler are distributed in Embedded Ch SDK in the directory CHHOME toolkit demos embedch Borland Assume Ch is installed in the directory C NCh To compile the code using Borland C compiler follow the procedures below CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 3 OTHER ESSENTIAL APIS AND OPTIONS FOR EMBEDDED CH e The command C Ch bin make exe does not work for Makefile for compiling and linking code in Borland C compiler Make sure the command make exe distributed and bundled with Borland compiler is used in Ch shell you can type command which make to check which make ex is invoked when in Ch shell Assume Borland compiler version 5 5 is used the Ch statement below sets up the command search path
221. ded by the APIs Ch AppendParseScriptO Ch AppendParseScriptFile Ch AppendRunScript Ch AppendRunScriptFile For example a global variable s of int type can be declared by Ch AppendParseScript interp int s Unlike global variables system variables remain in an instance of an Embedded Ch engine when a new program is loaded A system variable is declared in Ch with the type qualifier declspec global as shown below declspec global int s A system variable can be declared using the API Ch DeclareVar as shown below 44 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 5 REMOVING AND REDECLARING VARIABLES File Name removevar c include lt stdio h gt include lt embedch h gt int main ChInterp t interp int status char argvv removevar ch NULL Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program removevar ch failed n Ch ExprEval interp printf var d n var if Ch_SymbolAddrByName interp var Ch AppendParseScript interp pragma remvar var Ch AppendParseScript interp float var 100 0 Ch ExprEval interp printf var f n var Ch_AppendParseScript interp pragma remvar var Ch_AppendParseScript interp include lt array h gt Ch AppendParseScript interp array double var 2 3 1 2 3 4 5 6 Ch ExprEval in
222. ded library dl file so that some libraries will be included by default 113 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE member functions of Classl Classi Class1 void fptr fptr dlsym g sample dlhandle Classl Classl chdl if fptr NULL printf Error s dlsym s n class_func__ _dlerror return dlrunfun fptr NULL NULL Classi Class1 void fptr fptr dlsym g sample dlhandle Classi dClassl chdl if fptr NULL printf Error s dlsym s n class_func__ dlerror return dlrunfun fptr NULL NULL this return int Class1 memfunl int i void fptr int retval fptr dlsym g sample dlhandle Classi memfunl chdl if fptr NULL printf Error s dlsym s n class_func__ dlerror return 1 dlrunfun fptr amp retval memfunl this i return retval Program 5 43 Ch member function file sampclass chf target libsampclass dl libsampclass dl sampclass_chdl o ch dllink libsampclass dl cplusplus sampclass_chdl o sampclass_chdl o sampclass_chdl cpp ch dlcomp libsampclass dl cplusplus sampclass_chdl cpp clean rm f o dl exe Program 5 44 Makefile to create libsampclass dl for Unix Makefile dl 114 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FR
223. drByName interp sp printf sp2 gt i Sd n sp2 gt i 231 sd n number n is d n is a function of variable argument number n udinfo totnum amp meminfo i ize interp s udinfo tagname Chapter A APIs for Embedding Ch lt embedch h gt Ch DataType printf sp2 gt f f n sp2 gt f if Ch_DataType interp c CH CLASSTYP udtag Ch_UserDefinedTag interp c Ch UserDefinedInfo interp udtag amp udinfo printf udinfo dtype CH CLASSTYPE d n udinfo dtype CH CLASSTYPE printf class size of c is d n udinfo size printf class name of c is s n udinfo tagname if Ch DataType interp u CH UNIONTYPE udtag Ch UserDefinedTag interp u Ch UserDefinedInfo interp udtag amp udinfo printf union size of u is d n udinfo size printf union name of u is s n udinfo tagname 1 if Ch DataType interp sa CH STRUCTTYP printf sa is struct type n udtag Ch UserDefinedTag interp sa Ch UserDefinedInfo interp udtag amp udinfo printf struct size of sa is dWMn udinfo size printf struct name of sa is s n udinfo tagname if Ch ArrayType interp sa printf sa is an array n 1 sp struct tag Ch SymbolAddrByName interp sa printf sp gt i dMn sp gt i printf sp gt f f n sp gt f Sptt printf sp
224. ds to be used to execute the program The execution commands step 169 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER next and cont are similar If an Embedded Ch has not started yet the program will start an interpreter first by calling function chStart The event mask all contains the new line event CHMASKLINE and function return event CHMASKRET The event CHMASKRET is used to handle a breakpoint for function The expression dgpt r runmode with different values will be used inside the callback function to handle different commands To step over a function the function call event Ch MASKCALL and function return event Ch MASKRET are added to the event mask Functions chUpStack and chDownStack corresponding to commands up and down change the function stack of the executed program by calling the API Ch ChangeStack In a GUI based debugger the members of dgpt r calldata and the passed argument calldata ofthe API Ch ChangeStack containing the information of the functions in the stack could be used to update the displayed program in a GUI editor The current and highest possible stack levels can be obtained by the API Ch StackLevel The name in a stack can be obtained by the CPI Ch StackName Command stack corresponding to function chStack displays function member function or program name and corresponding stack level in each stack by calling function Ch StackName in a while loop Command
225. e Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp Ch interpreter argv File name and arguments of the Ch program The last field must be NULL Description When a program is run in Ch it first will be parsed then executed Ch RunScriptM performs these two steps together The function Ch RunScriptM executes a Ch program from C space The variable argv con tains the file name and arguments of the Ch program to be executed It must end with NULL in the last element of the variable The API Ch Initialize should be called before this function is called After function Ch RunScript is called variables and functions inside the script can be accessed by other APIs in the same interpreter interp However after function Ch RunScriptM is called variables and functions inside the script cannot be accessed by other APIs in the same inter preter interp But a single instance of Ch interpreter initialized by Ch Initialize can be used to process multiple Ch scripts using Ch ParseScript and Ch ExecScriptM or Ch RunScriptM Example Refer to Program 1 8 See Also Ch AppendParseScript Ch AppendRunScript Ch ParseScript Ch ExecScript Ch ExecScriptM Ch RunScript Ch Initialize Ch Home Ch_SymbolAddrByName Ch DataType Ch ExprEval Ch End 292 Chapter A APIs for Embedding Ch lt embedch h gt Ch SetGlobalUserData Ch SetGlobalUserData Synopsi
226. e lt embedch h gt int Ch ExecScriptM ChInterp t interp const char progname Purpose Execute a Ch program parsed by function Ch ParseScript Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter programe The program name the first element of the second argument of array of pointer to char in function Ch ParseScript Description When a program is run in Ch it first will be parsed then executed The function Ch ExecScriptM executes a Ch program parsed by function Ch ParseScript in C space The variable progname con tains the file name of the Ch program parsed by function Ch ParseScript The API Ch Initialize and Ch ParseScript should be called before this function is called After function Ch ExecScript is called variables and functions inside the script can be accessed by other APIs in the same interpreter interp However after function Ch ExecScriptM is called variables and functions inside the script cannot be ac cessed by other APIs in the same interpreter interp But a single instance of Ch interpreter initialized by Ch Initialize can be used to process multiple Ch scripts using Ch ParseScript and Ch ExecScriptM or Ch RunScriptM Example Refer to Program I 8 See Also Ch AppendParseScript Ch AppendRunScript Ch ExecScript Ch ParseScript Ch RunScript Ch RunScriptM Ch Initialize Ch Home Ch SymbolAddrByName
227. e of a variable or expression in the Ch space Declare system functions in the Ch space Change a system variable as a type specifier in the Ch space Declare system variables in the Ch space Delete a value node for an expression in the Ch space End the embedded Ch Run a parsed Ch program in C space Run a parsed Ch program in C space Calculate an expression in the Ch space Evaluate an expression in the Ch space Parse an expression in the Ch space Obtain the value of an expression in the Ch space Flush the contents of the buffer associated with a file descriptor Obtain the dimension of an array argument of a function Obtain the number of elements in the specified dimension of an array argument of a function Obtain the number of elements of array argument of a function Determine if an argument of function is an array 201 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgDataType Ch FuncArgFuncArgNum Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch FuncArgNum Ch FuncArgUserDefinedName Ch FuncArgUserDefinedSize Ch FuncType Ch GetGlobalUserData Ch GlobalSymbolAddrByIndex Ch GlobalSymbolAddrByName Ch GlobalSymbolNameByIndex Ch GlobalSymbolIndexByName Ch GlobalSymbolTotalNum Ch InitGlobal Var Ch Initialize Ch IsFunc Ch IsFuncVarArg Ch IsVariable Ch ParseScript Ch Reopen Ch RunScript Ch RunScriptM Ch SetGlobalUserData Ch Se Var Ch StackLevel Ch StackName Ch SymbolAddrByIndex
228. e sys tem functions defined by Ch DeclareFunc will be used Like a global function a system function prototype can be declared multiple times An identifier can be declared as both global and system variable In this case the identifier will be treated as a global variable inside a program For a variable of function type only one function definition can be defined if the identifier is used for both global and system variable The argument funcptr is a wrapper function to interface the binary function The wraper function is the same as that described in Ch SDK User s Guide Wrapper functions can be generated automatically from a header file using the utility program c2chf in Ch SDK Two Special Cases Functions returning a struct type and functions with a variable number of arguments are two special cases One have to pay attention to the two special cases when calling binary function from a Ch program in the Embedded Ch As shown in function func4 in the example below for a function returning a struct type the return type for the wrapper function is void The returned structure is passed as an extra first argument of pointer to struct For a function with a variable number of arguments the list of arguments in the Ch space is changed to the list of arguments in the C space by the function Ch VaVarArgsCreate and the used temporary memory is cleaned by the function Ch VaVarArgsDelete as shown in function func5 the example below
229. eam freopen stderrfile w stderr Before the error message generated by function call of Ch ParseScript can be retrieved from file stderrfile the file descriptor for the redirected stderr stream has to be closed by function call Ch Close interp fildes stderr which is equivalent to fclose stream The argument of function printf in script reopen ch shown in Program 1 13 is missing a closing double quote When Program is executed in a Unix machine it will display the following messages in the console 22 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 6 REDIRECTING STANDARD OUTPUT AND ERROR STREAM FROM EMBEDDED CH Error parse program reopen ch failed ERROR missing ERROR argument 1 of the function is undefined or not a valid expression ERROR syntax error before or at line 6 in file reopen ch gt return 0 BUG return lt The contents of the temporary file var tmp aaaxlaa6M are displayed by function displayContentsInFile in Program 23 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 6 REDIRECTING STANDARD OUTPUT AND ERROR STREAM FROM EMBEDDED CH File Name reopen c redirect stderr from Embedded Ch to different file include lt embedch h gt include lt stdio h gt include lt stdlib h gt include lt string h gt Display contents ina file void displayContentsInFile char filename FILE stream char buffer BUFSIZ int length stream
230. eclareFunc interp double funcl double x funcl chdl Ch DeclareFunc interp double func2 double x double y func2 chdl as shown in Program 5 12 The second argument of the function Ch DeclareFunc is the function prototype in the C space The function prototypes for wrapper functions funcl_chdl and func2 chdl in Program 5 7 passed as the third argument of Ch DeclareFunc are placed in a header file sample chdl h in Program 5 13 This header file is included in Program 5 12 Because function files funcl chf and func2 chf in Programs and 5 6 respectively are not needed in this case The header file sample h can be simplified as shown in Program 5 14 All other files including program ch func c sample chdl c and makfiles in section are the same The source code for this section is available in the directory CHHOME toolkit demos embedch chapters chapter5 declarefunc 86 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 2 CALLING C FUNCTIONS IN MAIN PROGRAM FROM CH WITHOUT FUNCTION FILES File name prog c include lt stdio h gt include lt embedch h gt include sample h include sample_chdl h int main ChInterp t interp char argvv program ch NULL int status Call funcl in func c double d d funcl1 3 0 printf funcl 3 0 in prog c f n d initialize embedded Ch Ch Initialize amp interp NULL declare functions funcl and func2
231. ectory Add the directory CHHOME extern include and the current working directory to the header file search path Click Project gt Properties it will pop up an Properties pages win dow From left panel click Configuration Properties gt C C gt General select Additional Include Directories from right panel Add the include path C Ch extern include e Take one of the following actions a Add compilation option MD Click Command Line under C C then select Additional Options Add MD The application program should be linked with Embedded Ch libraries chsdk 1iband embedch lib located in the directory CHHOME extern lib To add this directory in the library search path as follows Under the same Configuration Properties Window select Linker select Additional Library Directories from right panel Add C Ch extern 1ib then click Apply to finalize the settings in Property Window Click Command Line under Linker then se lect Additional Options Add chsdk lib embedch lib Finally click OK to finalize the settings in Project Settings b Add compilation option MDd and use Embedded Ch libraries chsdk mdd lib and embedch_mdd 1ib c Add compilation option MT and use Embedded Ch libraries chsdk mt lib and embedch_mt lib c Add compilation option MTd and use Embedded Ch libraries chsdk mtd lib and embedch mtd lib The source code for Visual NET project files embedfuncmain vcproj and embedclassm
232. ed library when the code is parsed The process for compiling and linking of programs can be automated by a Makefile program Make files in Programs 5 25 and 5 26 can be used to create executable program prog exe and dynamically loaded library 1ibsample dl used in this example for Unix respectively The similar makefiles in in Programs 5 27 and 5 28 can be used for Windows Similar to calling C functions in a library for command dllink the option embedded is required to make dynamically loaded library dl file so that some libraries will be included by default Using VC in NET in Windows the libname following the command dlcomp must have a surfix dl so that the source code will be compiled to use the multi thread dynamically loaded system library 95 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 4 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE File prog4 c include lt stdio h gt include lt string h gt include lt embedch h gt EXPORTCH void callback chdl void varg char list stringl string2 NULL void printlist char list typedef void FUNCPTR char int main int argc char argv ChInterp t interp ChOptions t option int status char argvv embedch4 ch NULL Ch Initialize amp interp NULL Ch DeclareFunc interp void callback void func char list ChFuncdl t callback chdl status Ch RunScript interp argvv
233. edded Ch is installed Ch VaArg Obtain a value of a function argument in Ch passed to a dynamically loaded library 204 Chapter A APIs for Embedding Ch lt embedch h gt Ch VarArgsAddArg Add an argument into a Ch style variable argument list Ch VarArgsAddArgExpr Add an argument in expression into a Ch style variable argument list Ch VarArgsDelete Delete a Ch style variable argument list and release its memory Ch VaEnd For a normal return from a function Ch VaStart Obtain an instance of Ch interpreter and initialize a handle for reference to arguments of a function in Ch Ch Version Obtain the version information for Ch Macros The return value for most Embedded Ch APIs has the following values defined inside the header file ch h Value Description CH ABORT 1 when Ch Abort is called CH ERROR for failure of a Ch SDK API call CH OK O for success of a Ch SDK API call 205 Chapter A APls for Embedding Ch lt embedch h gt Ch Abort Ch Abort Synopsis include lt embedch h gt int Ch Abort ChInterp t interp Purpose Abort the execution of a embedded Ch program Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter Description When Ch End interp is called the instance of the interpreter interp is completly deleted and its related memory is freed After Ch End interp is called no Ch SDK or Embedded Ch SDK API shall be called for that
234. edded Ch program funcarg ch are obtained in the hosting C program funcarg c funcarg c A C program to get information for global variables in Ch KKK kk kk k kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kok k k Ck k k kc k k ck ck ck k kok ck ck I k File Name funcarg c OKCKCKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCkCKCKCKCkCKCKCKCKCKCkCKCkCKCk ck k ck k k ck kck ck kk k ok ko ke ke e ke include lt stdio h gt include lt embedch h gt struct tag int i double f v int main ChInterp t interp int status char argvv funcarg ch NULL struct tag sp sp2 261 Chapter A APls for Embedding Ch lt embedch h gt Ch FuncArgDataType Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program funcarg ch failed n if Ch_FuncArgDataType interp f1 0 CH INTTYPE printf arg 1 of fl is int Wn if Ch_FuncArgDataType interp f1 1 CH UNDEFINETYPE printf arg 2 of fl is undefined Wn if Ch FuncArgDataType interp f2 0 CH UNDEFINETYPE printf arg 1 of 2 is undefined Wn if Ch FuncArgDataType interp f3 1 CH DOUBLEPTRTYPE printf arg 2 of 3 is double n if Ch FuncArgDataType interp f3 2 CH CHARPTRT
235. el if dgptr gt progname NULL printErrorNoFile return 1 interp dgptr gt interp level dgptr gt level chPrintSymbolValuesInStack interp return 0 chVariables debugInfo t dgptr ChInterp t interp int level ChBlock t calldata2 if dgptr gt progname NULL printErrorNoFile return 1 interp dgptr gt interp level dgptr gt level chPrintSymbolValuesInStack interp while Ch ChangeStack interp chPrintSymbolValuesInStack interp return 0 s n S s level level name Nn level classname name level name level level amp calldata2 CH OK level Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 183 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER add an expression or variable to the watched list int chAddWatchExpr debugInfo t dgptr char exprname watchExpression t temp dgptr gt wexpr watchExpression t wexpr watchExpression t malloc sizeof struct Watch wexpr exprtype CH UNDEFINETYPE not used wexpr gt exprname strdup exprname wexpr gt exprvalue NULL not used wexpr next NULL if temp NULL no element in the linked list dgptr gt wexpr wexpr else get the tail of the linked list while temp next temp t
236. emberfunc 1 member funct 0 not member funct int isconstructor 1 constructor 0 not constructor int isdestructor 1 destructor 0 not destructor int isvararg 1 funct with variable num arguments 0 not int arraytype one of array types int dim array dim int extent 7 extent for each dimension up to 7 int isbitfielg 1 bit field 0 not bit field int fieldsize struct tag int i 3 j 10 fieldsize of j is 10 int fieldoffset struct tag int i 3 j 10 fieldoffset of j is 3 ChUserDefinedTag t udtag tag for a member of struct class union j ChMemInfo t 311 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedMemlInfoB yIndex Field index gives the index number of the member in the symbol table for the user defined type The index number for the first member is 0 Field memname gives the name of the member Field offset gives the offset of the address for the member in the memory from the beginning of the variable of the user defined type Field dt ype gives the data type of the member Field ispublic is 1 if the member is public If the member of class is private it is 0 Field isfunc is 1 if the member is function type including function pointer to function and member function Othewise it is 0 Field ismemberfunct is 1 if the member is a member function constructor or destructor of a class Otherwise it is 0 If the member if a poin
237. emp gt next add the new one to th nd of the list temp gt next wexpr return 0 pricocess expressions and variables in the watched list int chProcessWatchExpr debugInfo t dgptr watchExpression_t wexpr dgptr gt wexpr while wexpr chPrint dgptr wexpr gt exprname wexpr wexpr gt next return 0 remove an expr from the watched list int chRemoveWatchExprs debugInfo_t dgptr char exprname watchExpression_t tmpl tmp2 tmpl dgptr gt wexpr if tmpl NULL return 0 else if strcmp tmpl gt exprname exprname dgptr gt wexpr tmpl gt next free tmpl gt exprname if tmpl gt exprvalue free tmpl gt exprvalue free tmpl Expression Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 184 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER else while tmpl next if strcmp tmpl gt exprname exprname tmp2 tmpl gt next tmpl gt next tmpl gt next gt next free tmp2 gt exprname if tmp2 gt exprvalue free tmp2 gt exprvalue free tmp2 return 0 tmpl tmpl gt next return 0 remove all exprs from the watched list int chRemoveWatchExprs debugInfo t dgptr watchExpression t tmpl head head dgptr gt wexpr while head tmpl head head head gt next free t
238. ent for each dimension up to 7 1 bit field 0 not bit field struct tag int i 3 j 10 fieldsize of j is 10 struct tag int i 3 j 10 fieldoffset of j is 3 tag for a member of struct class union destructor 0 not The following functions are declared in the header file embedch h Function Ch Abort Ch AddCallback Ch AppendParseScript Ch AppendParseScriptFile Ch AppendRunScript Ch AppendRunScriptFile Ch ArrayDim Ch ArrayExtent Ch ArrayNum Ch ArrayType Ch ChangeStack Ch Close Ch DataSize Ch DataType Ch DeclareFunc Ch DeclareTypedef Ch DeclareVar Ch DeleteExprValue Ch End Ch ExecScript Ch ExecScriptM Ch ExprCalc Ch ExprEval Ch ExprParse Ch ExprValue Ch Flush Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgArrayNum Ch FuncArgArrayType Description Abort the execution of a embedded Ch script Add events and callback function Append and parse a fragment of a Ch code in C space Append and parse a Ch program file in C space Append and Run a fragment of a Ch code in C space Append and Run a Ch program file in C space Obtain the dimension of an array Obtain the number of elements in the specified dimension of an array Obtain the number of elements of an array Determine if the variable is an array Change the stack function of an executed Ch program Close a file descriptor Get the size a variable or expression in the Ch space Get the data typ
239. erpreter Functions and variables in each interpreter can be accessed by other threads or the main thread This multi thread binary C program can be compiled to run portably across different platforms in Windows and Unix See Also Ch_GetGlobalUserData 294 Chapter A APIs for Embedding Ch lt embedch h gt Ch SetVar Ch SetVar Synopsis include lt embedch h gt int Ch SetVar ChInterp t interp const char name ChType t atype int Ch SetVar ChInterp t interp const char name Ch Type t atype type value int Ch SetVar ChInterp t interp const char name ChType t atype const char tagname type value int Ch SetVar ChInterp t interp const char name ChType t atype ChType t etype type value int dim int extentl Syntax Ch SetVar interp name atype value Ch SetVar interp name CH INTTYPE value Ch SetVar interp name CH PROCPTRTYPE NULL Ch SetVar interp name CH PROCPTRTYPE Ch SymbolAddrByName interp fp Ch SetVar interp name CH STRUCTTYPE tagname structvalue Ch SetVar interp name CH CLA SSTYPE tagname classtvalue Ch SetVar interp name CH UNIONTYPE tagname unionvalue Ch SetVar interp name CH CARRAYTYPE etype dim extentl Ch SetVar interp name CH CHARRAYTYPE etype dim extentl Purpose Assign a value in the C space to a variable in the Ch space Return Value This function returns CH OK on success and CH ERROR on failure Paramet
240. ers interp A Ch interpreter name The variable name in the Ch space atype The type of the argument to be added The values for data types ChType t are defined in header file ch h They are listed in Ch SDK User s Guide The argument to be added Description This function is typically used to access values and data in the C space from the Ch space The memory in the C space can be shared by variables in the Ch space through this function The vari ables in the Ch space can be either in a Ch script file or created dynamically at runtime by one of functions Ch AppendParseScriptFile Ch AppendParseScript Ch AppendRunScriptFile Ch AppendRunScript The data type and argument for function Ch_SetVar are similar to 295 Chapter A APIs for Embedding Ch lt embedch h gt Ch SetVar those for function Ch VarArgsAddAr rg described in detail in Ch SDK User s Guide Example In this example the values and their addresses of variables of different data type in program setvar cppinthe C space are shared by variables in Ch script set var ch in the Ch space Using Ch SetVar in the space the address of function func is assigned to variable fp both in the Ch space Ch function callback iscalled by function Ch CallFuncByName interp callback amp retval inthe C space setvar cpp A C program to share the data in C space with variables in Ch space KK kk kk kk kk kk kk kk kk kk kk kk kk kk kk KCKCKCKCKCKC
241. es The index number for the symbol table starts with 0 Example See Programs 2 4 and See Also Ch_SymbolTotalNum Ch_SymbolAddrByName Ch_SymbolAddrByIndex Ch_SymbolNameByIndex 306 Chapter A APIs for Embedding Ch lt embedch h gt Ch_SymbolNameByIndex Ch_SymbolNameByIndex Synopsis include lt ch h gt char Ch SymbolNameByIndex ChInterp t interp int num Purpose Get the symbol name of a variable in the symbol table for variables and arguments of a function or in the symbol table for global variables in a Ch program Return Value The address for the symbol name of a variable in a Ch program If the index number is out of the valid range for its symbol table the function returns NULL Parameters interp A Ch interpreter num The index number of a variable in the symbol table in a Ch program Description Based on its index number of a variable in the symbol table the function Ch_SymbolNameByIndex obtains the address for the symbol name of aglobal variable inside a dynamically loaded library or embedded Ch program If the control flow of the Ch program is inside a function the symbol table contains local variables including function arguments within its scope inside the function If the control flow of the Ch program is inside a member function of a class the symbol table contains local variables including members of the class and function arguments within its scope inside the function If the control f
242. fopen filename r length fread buffer 1 BUFSIZ stream buffer length N0 if length gt 0 printf s n buffer use MessageBox NULL buffer Message title 0 for GUI in Windows fclose stream hInterp t interp int main G int status char argvv program reopen ch stderrfile L_tmpnam ChFile_t fildes_stderr Ch_Initialize amp interp NULL tmpnam stderrfile create a temporary file name fildes stderr Ch_Reopen interp stderrfile w STDERR FILENO i f fildes stderr CH I printf Error RROR redirect stderr error stream n reopen ch NULL dynamically m canno create char argvv argvv char malloc 2 sizeof char argvv 0 strdup program argvv 1 NULL Status Ch ParseScript interp argvv if status CH ERROR printf Error parse program s failed n program Ch Close interp fildes stderr displayContentsInFile stderrfile else status Ch ExecScript interp program execute reopen ch Ch_Close interp fildes_stderr if status CH ERROR printf Error execute script s failed n program displayContentsInFile stderrfile remove stderrfil free argvv 0 free argvv Ch End interp remove temporary file for stderr release memory and terminate the shell Program 1 12 Redirecting stderr to a file 24
243. for a user defined struct class or union type typedef struct ChUserDefinedInfo ChType t dtype user defined data type CH STRUCTTYPE CH CLASSTYPE CH UNIONTYPE or CH UNDEFINETYPE char tagname tag name int size size of class struct union int totnum total number of members in class struct union ChUserDefinedInfo t Field dt ype gives the name of the user defined data type If it is a structure the value for dt ype is ChSTRUCTTYPE If it is a class the value for dtype is ChCLASSTYPE If it is a union the value for dt ype is ChUNIONTYPE It will be CH UNDEFINETYPEif the input argument udt ag is NULL Field tagname gives the tag name of the user defined type It will be NULL if the input argument udtag is NULL Field size gives the size of the user defined type It will be 0 if the input argument udt ag is NULL Field tot num gives the number of members of the user defined type It will be 0 if the input argument udtag is NULL 309 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedInfo Example See Ch DataType Programs 2 9 and See Also Ch DataType Ch UserDefinedMemInfoByIndex Ch UserDefinedMemInfoByName Ch UserDefinedTag 310 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedMemlInfoB yIndex Ch UserDefinedMemInfoByIndex Synopsis include lt ch h gt int Ch UserDefinedMemInfoByIndex ChInterp t in
244. func2 in the Ch space calls the corresponding binary function in the C space The value for the global variable d in the Ch space is then obtained by Ch SymbolAddrByName The output from executing Program 5 8 is as follows d in the Ch space 10 000000 C header file sample h and program files unc c sample chdl c and prog c are compiled and linked to form an executable binary program prog exe so that function func1 can be called inside program prog c The process for compiling and linking of programs can be automated by a Makefile program Makefiles in Programs 5 9 and 5 10 can be used to create executable program prog exe used in this example for Unix and Windows respectively using utility programs dl comp and dllink provided in Ch SDK for portable compilation and linking Similarly program prog1 c is compiled and linked against func c and sample chdl c to create a binary executable program progl exe Makefile in Program 5 11 for Windows can also be used to create programs prog exeandprogl exe without using programs dlcomp and dllink 84 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE If application programs such as prog exe are created using other automation process such as inte grated development environment compile option DWINDOWS DLINUX DLINUXPPC DSOLARIS DHPUX DDARWIN DFREEBSD DONX should be added when programs such as sample chdl c
245. function If the Ch function to be called takes arguments they will be listed following the third argument retval The function returns zero on successful execution or non zero on failure C program funcall2 c in Program is an example of calling Ch functions with API Ch CallFuncByAddr The Ch program uncall2 ch shown in Program is similar to the one in Program 3 2 The difference is that the global Ch variable ret val g is not used in this case To get the return value of the Ch function the retval g is declared as an int in C space instead After the Ch program B 4 is executed the statement below fptr Ch SymbolAddrByName interp func obtain the address of Ch function func Then function call Ch CallFuncByAddr interp fptr amp retval g 10 50 CHAPTER 3 GALOMNG UKCF N CURING PROM ISRAGEUSING CH CALLFUNCB YNAME AND CH CALLFUNCB YNAMEV int ii 10 double dd 1 1 int func int argl printf Xnprinted from Ch function func ii Sd dd f n ii dd return 2 argl Program 3 4 Calling Ch function with API Ch CallFuncByAddr funcall2 ch printed from Ch function func ii 10 dd 1 100000 In C after the first function call retval_g 20 printed from Ch function func ii 11 dd 12 100000 In C after expression evaluations and the second function call retval_g 40 Figure 3 2 Output from executing funcall2 c invokes this Ch function by its addre
246. function The function name can be a function located in a function file specified by the function file path fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is the function name for Ch CallFuncByNameVar The third argument is the pointer containing the address of the return value of the called function The arguments of the called function are passed in the fourth argument arglis The argument arglis is built dynamically using functions Ch VarArgsAddArg Ch VarArgsAddArgExpr and Ch VarArgsCreate Later it shall be deleted by function Ch VarArgsDelete The function returns zero on successful execution or non zero on failure C program callfuncbynamevar c in Program 3 8 is an example of calling Ch functions with API Ch CallFuncByNameVar Program 3 9 is used as a Ch program in this example The program illustrates how arguments of scalar array computational array user defined data type such as structure class union are handled Function func in the Ch space is called by API Ch CallFuncByNameVar interp func amp retval arglist in C Program The first call uses variables in the Ch space as arguments It is equivalent to calling function func by func n ch m ch ch p ch ach b ch c ch d ch hs sp ch in the Ch space When function func is called the sec
247. function c2 i 50 y 0 tag constructor tag destructor funcl member function 4 50 y 0 tag constructor tag destructor funcl member function The stack level is 2 The current line number is 0 The current file name is cplus cpp Program cplus cpp at stack level 2 has 4 symbols i 40 g 100 0000 a 1 0000 2 0000 3 0000 4 0000 5 0000 main 0x4d010 Figure 7 7 Output from executing prog4 cplus cpp 159 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES funcl is called argument i in funcl is true event MASKEND is true The stack level is O0 The current line number is 17 Function funcl starts at line 8 Function funcl ends at line 18 The function name is funcl The current file name is nestedfunc ch Function funcl at stack level 0 has 6 symbols i 30 Z 0 SEUNG funcl g 200 0000 y 0 a 10 The stack level is 1 The current line number is 19 Function nestedfunc starts at line 4 Function nestedfunc ends at line 20 The function name is nestedfunc The current file name is nestedfunc ch Function nestedfunc at stack level 1 has 5 symbols z EURO gt nestedfunc g 300 0000 r 0 func1 local 0X51678 i 10 rhe stack level is 2 The current line number is 25 Function main starts at line 22 Function main ends at line 27 The function name is main The current file name is nestedfunc
248. g Ch UserDefinedTag Ch UserDefinedInfo 221 Chapter A APIs for Embedding Ch lt embedch h gt Ch ArrayExtent Ch ArrayExtent Synopsis include lt ch h gt int Ch_ArrayExtent ChInterp_t interp const char name int dim Purpose Obtain the number of elements in the specified dimension of a local or global variable of array type in the Ch space Return Value If the local or global variable is of array type This function returns the number of elements in a specified dimension of the array Otherwise it returns 0 Parameters interp A Ch interpreter name Name of the variable or expression in the Ch space dim An integer specifying for which dimension the number of elements will be obtained 0 for the first dimension Description This function returns the number of elements in the specified dimension of a local or global variable of array type in the Ch space For array a n m If 0 is passed to this function as the third argument the extent of the first dimension i e n will be returned Otherwise if 1 is passed to m will be returned Example See Ch_DataType See Also Ch_DataType Ch0 Ch_ArrayDim Ch_ArrayNum Ch_ArrayType Ch_FuncType Ch_FuncArgNum Ch_IsFuncVarArg Ch_UserDefinedTag Ch UserDefinedInfo 222 Chapter A APIs for Embedding Ch lt embedch h gt Ch ArrayNum Ch ArrayNum Synopsis include lt ch h gt int Ch_ArrayNum ChInterp_t interp const char name
249. g 3 of f3 is char arg 2 of f4 is int arg 3 of f4 is int arg 4 of f4 is int arg 5 of f4 is int arg 2 of 5 Ls ant arg 3 of f5 is int arg 4 of f5 is int arg 5 of f5 is int arg 6 of f5 is int arg 1 of f6 is struct arg 2 of f6 is struct arg 3 of f6 is class arg 4 of f6 is class arg 5 of f6 is union arg 6 of f6 is union arg 7 of f6 is struct arg 1 of f7 is int arg 2 of f7 is int arg 3 of f7 is int arg 1 of fl is not array arg 4 of fl is not array arg 1 of f2 is not array arg 2 of f4 is pointer to C array arg 3 of f4 is C VLA array arg 4 of f4 is C VLA array arg 5 of f4 is C VLA array arg 2 of f5 is pointer to C array arg 3 of f5 is Ch VLA array arg 4 of f5 is Ch VLA array arg 5 of f5 is Ch VLA array arg 6 of f5 is Ch VLA array arg 7 of f6 is pointer to C array arg 1 of f8 is Ch array 2 C arg of f8 is Ch VLA array arg 3 of f8 is Ch VLA array number of elements of arg 1 of fl is 0 number of elements of arg of fl is 0 number of elements of arg of f2 is 0 number of elements of arg of f4 is 6 number of elements of arg of Ba ys I of 4 is 2147483647 of 4 is 2147483647 of 5 is 6 number of elements of arg number of elements of arg number of elements of arg Nowe 05 PK nm gt 266 Ch FuncArgDataType Chapter A APls for Embedding Ch lt embedch h gt Ch FuncArgDataType number of elements of arg 3 of f5 is 1 number of elements of arg 4 of f5 is 2147483647 number of element
250. ger the callback function This field is valid only when the event contains the event mask Ch MASKCOUNT Field level gives the level of the function being executed Level 0 is the current running function whereas level n 1 is the function that has called level n For example when function level0 is be ing called it has level 0 Function 1evell which calls function 1evelO has level 1 Function level2 which calls function level in turn has level 2 void level2 levell void levell level0 void levelO this function is being executed The stack for the current function can be changed by the API Ch ChangeStack based on the function level Field 1inecurrent contains the current line number where the Ch program is executing When the stack is changed to the top level by Ch ChangeStack the level is 0 Field 1inefuncbegin contains the line number where the definition of the executed function begins Field 1inefuncend contains the line number where the definition of the executed function ends Field source contains the file name if the source of the executed code is a file Otherwise it is a string such as when the code is loaded from the memory by function Ch AppendRunScript If the code is from a string the source is a string up to 70 characters starting with 8st ring Field funcname contains the function name if the executed line of the code is within a function Otherwise
251. glist sp ch equivalent of retval func n ch m ch ch p ch a ch b ch eeh d ch chs sp ch h CallFuncByNameVar interp func amp retval arglist h VarArgsDelete interp arglist P M DP P P P 8 2 Con in in in in in in in US oQUO CO 070 GO QOO A Q Q use values in C space arglist Ch VarArgsCreate interp h VarArgsAddArg interp amp arglis h VarArgsAddArg terp amp arglis h VarArgsAddArg terp amp arglis h VarArgsAddArg terp amp arglis h VarArgsAddArg terp amp arglis h VarArgsAddArg terp amp arglis H INTTYPE 2 H INTTYPE H DOUBLETYE H DOUBLEPTRTYPE p H CARRAYTYPE CH INTTYPE a dim n m H CARRAYTYPE CH INTTYPE b dim n m Y Y ct ct ct ct ct ct T lt in in in in in in h VarArgsAddArg terp amp arglis H CARRAYTYPE CH INTTYPE c dim n m h VarArgsAddArg interp amp arglist H CARRAYTYPE CH INTTYPE d dim n m or Ch VarArgsAddArg interp amp arglist CH CHARRAYTYPE CH INTTYPE c dim n m Ch VarArgsAddArg interp amp arglist CH CHARRAYTYPE CH INTTYPE d dim n m h VarArgsAddArg interp amp arglist CH STRUCITYPE tag amp s h VarArgsAddArg interp amp arglist CH STRUCTPTRTYPE sp equivalent of retval func n m f p a b c d s sp h CallFuncByNameVar interp func amp retval argli
252. gptr gt argvv if status CH OK status Ch ExecScript interp dgptr gt argvv 0 return status int chStepInto debugInfo t dgptr int mask dgptr gt runmode RUNMODE STEPINTO if dgptr interp NULL return chStart dgptr RUNMODE STEPINTO NULL mask CH MASKLINE CH MASKRET return Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr gt clientdata dgptr count Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 181 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chStepOver debugInfo t dgptr int mask dgptr gt runmode RUNMODE STEPOVER if dgptr gt interp NULL return chStart dgptr RUNMODE STEPOVER NULL mask CH_MASKLINE CH_MASKCALL CH_MASKRET return Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr gt clientdata dgptr count int chContinue debugInfo t dgptr int mask dgptr runmode RUNMODE CONTINUE if dgptr gt interp NULL return chStart dgptr RUNMODE CONTINUE NULL mask CH MASKLINE CH MASKRET return Ch AddCallback dgptr gt interp mask dgptr gt callback dgptr gt clientdata dgptr count int chUpStack debugInfo t dgptr ChBlock t calldata if dgptr gt progname NULL printErrorNoFile return
253. gram OKCKCKCKkCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCkCKCKCKCkCKCkCKCkCKCkCk kk k ck k ck k k kk kk k ok Kok ke ke ex include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv accesschvar ch NULL int pii double pdd initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program accesschvar ch failed n if Ch DataType interp ii CH INTTYPE printf ii is int n else printf ii is not int n if Ch DataType interp dd CH DOUBLETYPE printf dd is double n else printf dd is not double n printf Ch DataSize interp dd dMn Ch DataSize interp dd pii Ch SymbolAddrByName interp ii Ch SymbolAddrByName interp dd pdd printf In C before assignments ii Sd dd f n pii pdd pii 20 pdd 2 2 printf In C after assignments ii d dd f n pii pdd Ch End interp Program 2 1 Accessing variables in a Ch program accesschvar c int ii 10 double dd 1 1 printf printed from accesschvar ch ii d dd f n ii dd Program 2 2 Accessing variables in a Ch program accesschvar ch 30 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 1 ACCESSING VARIABLES BY ADDRESSES pri
254. gram 7 2 C program with a callback function prog1 c Contd 133 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION event MASKCALL is true The stack level is 0 The current line number is 17 Function main starts at line 17 Function main ends at line 22 The function name is main The current file name is progl ch sp gt i 10 sp gt d 20 000000 event MASKCALL is true The stack level is 0 The current line number is 5 Function funcl starts at line 5 Function funcl ends at line 15 The function name is funcl The current file name is progl ch i 10 funcl is called event MASKBLOCK is true The stack level is 0 The current line number is 10 Function funcl starts at line 5 Function funcl ends at line 15 The function name is funcl The current file name is progl ch i 0 argument i in funcl is true event MASKEND is true The stack level is 0 The current line number is 14 Function funcl starts at line 5 Function funcl ends at line 15 The function name is funcl The current file name is progl ch i 30 event MASKRET is true The stack level is 0 The current line number is 15 Function funcl starts at line 5 Function funcl ends at line 15 The function name is funcl The current file name is progl ch i 10 event MASKRET is true The stack leve
255. gument lists The whole procedure is as follows Ch VarArgsCreate is used first to initialize a Ch style variable length argument list firt Then Ch VarArgsAddArg is used to add arguments with values in the C space into this list Or Ch_VarArgsAddArgVar is used to add arguments in the Ch space into the list Finally Ch VarArgsDelete is used to delete this list The result of running executable compiled from Program is shown as Fig ure 3 5 Function Ch CallFuncByNamew used in Ch CallFuncByName interp funcl amp retval ap ch and Ch CallFuncByName interp func3 amp retval ap ch in Program can be replaced by function Ch CallFuncByNameVar To use Ch_CallFuncByNameVar to call function func2 in the Ch space the first argument of function func2 should also be placed in the argument list The code ap ch Ch VarArgsCreate interp Ch VarArgsAddArg interp amp ap ch CH INTTYPE j Ch VarArgsAddArg interp amp ap ch CH FLOATTYPE f Ch CallFuncByName interp func2 amp retval i ap ch Ch VarArgsAddArg interp amp ap ch CH DOUBLETYPE d Ch CallFuncByName interp func2 amp retval i ap ch Ch VarArgsDelete ap ch in Program should be changed to ap ch Ch VarArgsCreate interp Ch VarArgsAddArg interp amp ap ch CH INTTYPE 15 Ch VarArgsAddArg interp amp ap ch CH INTTYPE 333 Ch VarArgsAddArg interp amp ap ch CH FLOATTYPE f Ch CallFuncByNameVar interp fu
256. h type int and dd with type double are assigned with original values of 10 and 1 1 respectively The pointer p is pointed to the address of the variable ii The array a and structure s are also declared and initialized Figure 2 7 illustrates how to evaluate expressions in the Ch program with embedded Ch In C program expreval c after the embedded Ch shell is initialized the Ch program is loaded and executed Statements below pii Ch SymbolAddrByName interp ii pdd Ch SymbolAddrByName interp dd p2 Ch SymbolAddrByName interp p pa Ch SymbolAddrByName interp a sp Ch SymbolAddrByName interp s obtain the addresses of the Ch global variables ii dd p a and s of int double pointer to int array of int and structure types respectively The function calls Ch ExprEval interp ii Ch ExprEval interp dd dd ii 2 1 pow 2 0 3 Ch ExprEval interp func evaluate expressions ii dd func dd ii 2 1 pow 2 0 3 37 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS KKK kk kk kk kk kk kk kk kk kk kk kk kk KCKCKCKCKCKCKCKCKCKCKCKCKCk kk k k k Ck kk kk ck k ck k ck k KKK OK File Name userdefinedsymbol c CKCKCKCKkCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCkCKCkCKCKCKCkCKCkCKCkCKCk Ck k ck k ck k ck k k k kk k ok Kok ke ke ex include lt stdio h gt include lt embedch h gt int main ChInterp t interp i
257. h 1ib in Windows or 1ibembdch a in other platforms to form program prog exe Program 5 35 is the Ch program we want to invoke from C space with embedded Ch It includes header file sampclass hin Program 5 29 in Ch space The definitions of the constructor destructor and member function for class Class1 in Ch space defined in Program 5 36 is included by header file sampclass h using the directive pragma importf lt sampclass chf gt Class member function file sampclass chf should be located in a directory specified in the system variable _fpath for function files Like interface C functions in an application program from Ch scripts the handler g sample dlhandle for the dynamically loaded module is obtained by the programming statement g sample dlhandle dlopen NULL RTLD LAZY However the dynamical loading of a module can be placed inside in a constructor instead of in a header file as shown in Program 5 36 Similarly the loading of the program itself with the value of NULL for handler g sample dlhandle in HP UX is treated differently from other platforms When a class is instantiated at the file scope the constructor will be invoked at the parsing stage There fore the constructor C1ass1 Class1 will open the dynamically loaded library 1ibsampclass dl and get the address of its binary counter part using generic built in functions dlopen and dlsym respectively The constructor Class1 Classl destructor Class1
258. h FuncArgIsFuncVarA rg Ch FuncArgFuncArgNum Ch FuncA rgUserDefinedSize 273 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgUserDefinedSize Ch FuncArgUserDefinedSize Synopsis include lt ch h gt int Ch_FuncArgUserDefinedSize ChInterp_t interp const char funcname int argnum Purpose Obtain the size of a user defined data type in terms of struct class or union and its pointer type for an argument of function in the Ch space Return Value If the argument of the function in the Ch space is a user defined data type this function returns the size of the user defined data type Otherwise it returns 1 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description This function returns the size of struct class union or its pointer type for an argument of function in the Ch space Function Ch_FuncArgUserDefinedName can be used to obtain the address of the tag name for the user defined type Example See Ch_FuncArgDataType See Also Ch FuncArgDataType Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch_FuncArgArrayType ChFuncArgIsFunc Ch_FuncArgIsFuncVarArg Ch_FuncArgFuncArgNum Ch_FuncArgUserDefinedName 274 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncType Ch FuncType Synopsis include lt ch h gt ChFuncType t Ch_FuncType
259. h the value for c_i in the Ch space by function call Ch SetVar interp ch i CH INTTYPE c i 55 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 5 CALLING CH FUNCTIONS BY CH CALLFUNCBYNAMEVARO Details about using function Ch SetVar to set variables in the Ch space are described in section 2 2 The expression and formulas including functions in function files in the Ch space can be calculated by either functions Ch ExprEval or Ch ExprCalc Function Ch ExprCalc can pass back the result of the calculated expression Program 3 7 the expression contains function hypot in a function file in function call Ch ExprCalc interp 10 ch_ithypot 3 4 CH DOUBLETYPE amp retval The output from Program 3 7 is given as follows retval 5 000000 hello retval 20 000000 retval 505 000000 3 5 Calling Ch Functions by Ch CallFuncByNameVar Both functions Ch CallFuncByAddr and Ch CallFuncByName can be used to call functions with different number of arguments However a function to be called by these two APIs has to be hardcoded API Ch CallFuncByNameVar can be used to call different functions with different arguments dynamically The prototype of API Ch CallFuncByNameVar is given in header file ch h as follows int Ch CallFuncByNameVar ChInterp t interp char name void retval void arglist The first argument is an instance of Ch interpreter The second argument is a string containing the name of the
260. hPrintSymbolValue ChInterp_t interp char name void addr int i j dim n m size nestlevel 1 ChType_t datatype ChFuncType_t functype ChUserDefinedTag_t udtag Ch_DataType interp name Ch FuncType interp name datatype functype Program 7 9 Functions for printing values of all variables in a stack stack c 149 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES if functype if functype CH FUNCPTRTYPE pointer to func printf 16sOX p n name void addr else if functype CH FUNCTYPE char funcname if stremp name dlclose strncmp name dlclose 9 strncmp name Ch 4 amp amp displayAll suppress dlclose math etc funcname char malloc strlen name strlen local 1 strcpy funcname name strcat funcname if Ch VarType interp name CH LOCALVARTYPE strcat funcname local local function printf 16sO0X p n funcname void addr free funcname else if displayAll if functype CH FUNCPROTOTYPE do not print func prototype printf 16s name if functype CH FUNCCONSTYPE printf constructorin else if functype CH FUNCDESTTYPE printf destructor n else if functype CH FUNCMEMBERTYPE printf member function n else if Ch_ArrayType interp name if strcmp name __func__ s
261. hType_t memtype char format 16 expr 8 void memaddr ChUserDefinedInfo t udinfo ChMemInfo t meminfo format is Xn 14s or Xn 12s strcpy format n indent 16 for i 0 i lt nestlevel i strcat format indent 2 sprintf expr Sd indent strcat format strcat format expr strcat format s Ch UserDefinedInfo interp udtag amp udinfo for index 0 index lt udinfo totnum index t Ch UserDefinedMemInfoByIndex interp udtag index amp meminfo if meminfo isconstructor if displayAll printf format meminfo memname printf constructor else if meminfo isdestructor if displayAll printf format meminfo memname printf destructor else if meminfo ismemberfunc if displayAll printf format meminfo memname printf member function Program 7 9 Functions for printing values of all variables in a stack stack c Contd 151 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES else printf format meminfo memname memtype meminfo dtype memaddr char addr meminfo offset if meminfo udtag member is struct class or union nestlevel if meminfo arraytype if meminfo dim 1 for i 0 i lt meminfo extent 0 i if memtype CH_CHARTYPE amp amp char memaddr 0 break skip the rest char
262. he callback function When the program is executed by the command start step next or cont the conditional expression of the if statement if calldata event amp CH MASKLINE will be true If the statement is executed by the command step or next the function chProcessActionMenu will be called to obtain the user s input The flow of the program control will return from the callback function If the program is executed by the command start or cont all breakpoints in the linked list will be searched by a while loop against the current executed statement If a breakpoint was set for a file at the specified line number the file name and line number in the breakpoint and current statement will be compared If they are matched the optional conditional expression for the breakpoint is checked by the function chTestBreakPointCondition If this function returns 0 the breakpoint is hit and the function chProcessActionMenu will be called to process the user input For a function breakpoint it will test if the current function matches with the function set in the breakpoint Only when the function is entered the first time the breakpoint will be hit When the first executable line of the function with a breakpoint is executed the field called for the breakpoint is set to 1 by the statement bp gt u func called TRUE so that the function will not be stopped during its execution The field called will be set to 0 when the function returns so
263. he index number is out of the valid range for its symbol table the function returns NULL If the symbol is a tag name for class structure or union the function returns NULL For a variable of function prototype without function definition or an extern variable without definition the function returns NULL For a variable of C array Ch computational array or pointer to Ch computational array the returned value is the address of the first element For a variable of pointer to C array the returned value is the address of the pointer to C array i e the address of the first element For example for variables of a and p declared in Ch int pa 3 p al2 3 the returned values for both pa and p are the same of pointer to pointer to int The return value for a is the address of the first element a 0 0 The data type for variable a returned from Ch_DataType is CH INTTYPE whereas the data type for variables pa and p returned from Ch DataType is the same as CH INTPTRTYPE Note that in C the address of function is the same as the function For example the output from the two printing statements below are the same unb DURON Jy o A printf address of func p n func printf address of func So n amp func Parameters interp Ch interpreter num The index number of global variable in the symbol table in a Ch program Description Based on its index number of a variable in the symbol table the function Ch SymbolAddrByI
264. he structure for an expression in the watch list typedef struct WatchExpression ChType t exprtype expression type reserved for future use char exprname expression name void exprvalue expression value reserved for future use struct WatchExpression next next expression in the linked list watchExpression t Program 7 13 The header file with defined data structures and function prototypes for the debug program debug h Contd 175 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER The structure for information for debug typedef struct char progname a Ch program to be debugged char argvv MAX_NUM_ARGS a Ch program with command line option to be debugged ChInterp t interp Ch interpreter ChOptions t option Ch initialization options for Ch Initialize interp option void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata ChPointer t clientdata ChBlock t calldata callback data int count count value for the callback int level stack level runmode t runmode run mode from a menu breakPoint t bkpt the head of breakpoints watchExpression t wexpr the head of watch expressions char prevfuncname previous function name for handling stop in int stepovernum number of functions stepped over by step over debugInfo t functions
265. hell File descriptor for the fourth argument of function Ch Reopen STDIN FILENO Standard input STDOUT FILENO Standard output STDERR FILENO Standard error output Event mask for the second argument of function Ch AddCallback The mask specifies on which events the callback will be called Ch MASKNONE No callback Ch MASKCALL Just after Ch calls and enters a new function Ch MASKRET Just before Ch leaves the function Ch MASKBLOCK Just after Ch enters a new block Ch MASKEND Just before Ch leaves the block Ch MASKLINE When Ch is about to start the execution of a new line of code or when it jumps back in the code even to the same Ine Ch MASKCOUNT After Ch executes every count instructions Ch MASKABORT When Ch is aborted by Ch Abort One of return values for function Ch ChangeStack CH INVALIDLEVEL invalid function level The data type ChFuncType t defined inside the header file embedch h has the following values Value Description CH NOTFUNCTYPE not function CH FUNCTYPE regular function CH FUNCPROTOTYPE function prototype without function definition CH FUNCPTRTYPE pointer to function CH FUNCMEMBERTYPE function of a class CH FUNCCONSTYPE constructor of a class CH FUNCDESTTYPE destructor of a class The data type ChVarType t defined inside the header file embedch h has the following values 203 Chapter A APIs for Embedding Ch lt embedch h gt Value Description CH NOTVARTYPE not
266. hould be deleted later by this function Ch DeleteExprValue When the value node corresponding to the result of an expression is deleted the result cannot be accessed any more Example See function chPrint in Program See Also Ch SymbolAddrByName Ch DataTypeO Ch CallFuncByAddr Ch_CallFuncByName Ch ExprCalc Ch ExprParse Ch ExprEval Ch ExprValue 244 Chapter A APIs for Embedding Ch lt embedch h gt Ch End Ch End Synopsis include lt embedch h gt int Ch End ChInterp t interp Purpose End embedded Ch Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter Description The function Ch End ends embedded Ch which is initialized by the API Ch Initialize It is mandatory if the API Ch Initialize is called Normally functions registered in atexit for Embedded Ch programs are executed in Ch End When a Ch script is executed by Ch ExecScriptM or Ch RunScriptM registered functions in atexit will be exe cuted when the script is executed Therefore a single instance of Ch interpreter initialized by Ch Initialize can be used to process multiple Ch scripts using Ch ExecScriptM or Ch RunScriptM Example Refer to Program I 1 See Also Ch Abort Ch Initialize Ch Home Ch SymbolAddrByName Ch DataType Ch ExprEval Ch RunScript 245 Chapter A APIs for Embedding Ch lt embedch h gt Ch Exec
267. i dMn sp gt i printf sp gt f fMn sp gt f if Ch DataType interp b CH INTTYPE printf b is a value of int in if Ch_ArrayType interp b CH CHARRAYTYPE printf b is a Ch computational array n printf Array dim of b is dWMn Ch_ArrayDim interp b printf Array elements of b is d n Ch ArrayNum interp b printf Extent of the 1st dim of b dWMn Ch ArrayExtent interp b 0 printf Extent of the 2nd dim of b dWMn Ch ArrayExtent interp b 1 bp int Ch SymbolAddrByName interp b printf bp din bp if Ch_DataType interp b2 CH INTTYPE printf bp is a value of int n if Ch_ArrayType interp b2 CH CHARRAYVLATYPE printf b2 is a Ch VLA array n printf Array dim of b2 is d n Ch ArrayDim interp b2 printf Array elements of b2 is dWMn Ch ArrayNum interp b2 printf Extent of the 1st dim of b2 d n Ch ArrayExtent interp b2 0 printf Extent of the 2nd dim of b2 d n Ch ArrayExtent interp b2 1 if Ch_DataType interp arrayfunc CH DOUBLETYPE printf arrayfunc return type is double n if Ch_ArrayType interp arrayfunc CH_CHARRAYTYPE 232 Chapter A APIs for Embedding Ch lt embedch h gt Ch DataType printf arrayfunc is Ch array n printf Array dim of arrayfunc is d
268. iate an old one with the new one Run a Ch program in C space Run a Ch program in C space Set the global user data to be obtained by Ch GetGlobalUserData Set a value in C space to a variable in Ch space Obtain the highest possible level of the stack Obtain the name in a stack Get the address of a variable based on its index in the symbol table Get the address of a variable based on its name within its scope Get the name of a variable based on its index in the symbol table Obsolete use Ch SymbolIndexByName Obtain the index num of a variable in the symbol table based on its name Obtain the total number of symbols for variables in the symbol table Obtain the information for a user defined struct class union type Obtain the information for a member of variable of user defined type Obtain the information for a member of variable of user defined type Obtain the name of the user defined data type of a variable Obsolete use Ch UserDefinedInfo Obtain the size of the user defined data type of a variable Obsolete use Ch UserDefinedInfo Obtain the tag for a variable of user defined struct class union type Determine if a symbol is a global or local variable The following macros are defined in the header file embedch h 202 Chapter A APIs for Embedding Ch lt embedch h gt Macro Description Macros for member field shelltype of structure ChOpt ions CH REGULARCH Use regular shell CH SAFECH Use safe s
269. im Ch FuncArgArrayExtent Ch FuncArgArrayType Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch FuncArgUserDefinedName Ch FuncArgUserDefinedSize 269 Chapter A APIs for Embedding Ch lt embedch h gt Ch_FuncArgIsFunc Ch FuncArgIsFunc Synopsis include lt ch h gt int Ch_FuncArgIsFunc ChInterp_t interp const char funcname int argnum Purpose Determine if an argument of a function in the Ch space is a function or pointer to function Return Value If the argument is a function or pointer to function this function returns 1 Otherwise it returns 0 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description The function Ch FuncArgIsFunc determines if the argument is a function or pointer to function Example See Ch_FuncArgDataType See Also Ch FuncArgDataType Ch FuncArgArrayType Ch FuncArgArrayDim Ch FuncArgArrayExtent Ch FuncArgIsFuncVarArg Ch FuncArgFuncArgNum Ch FuncArgUserDefinedName Ch FuncA rgUserDefinedSize 270 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgIsFuncVarArg Ch FuncArgIsFuncVarArg Synopsis include lt ch h gt int Ch_FuncArgIsFuncVarArg Chinterp_t interp const char funcname int argnum Purpose Determine if an argument of a function in the Ch space is a function or pointer to function with
270. in Program 7 2 It then sets the variable y in Program 7 12 with the value 50 for the variable j in the C space using function set Variable In an application the name of the variable might be obtained interactively Before it is assigned a value it needs to be checked if it is a valid Ivalue by functions Ch VarType and Ch FuncType Afterwards expressions i y and 2 i r g are evaluated by function exprCalculation At this point the values for variables i y r and g are 30 50 200 and 500 respectively Function exprCalculation first tests if an expression is valid or not by the API Ch ExprParse then calculate the expression using the API Ch ExprCalc and finally display the expression and its value After calling function Ch ChangeStack with the stack level 1 the program stack goes to the calling function main Function printCalldata is called again to display the information of the function main The local variable x in the function main and and global variable r in the Ch space are assigned with the values of variables j and d in the C space respectively At this point the values for variables i x r and g are 10 50 500 and 100 respectively After the callback function is finished printf statements in Program 7 12 display the values of local and global variables as shown in Figure 7 9 The ideas presented in Program 7 8 can also be used to handle variables of user defined data type As an example after obta
271. in a Ch program symbol c 32 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 2 ASSIGNING VALUES IN C SPACE TO VARIABLES IN CH SPACE printed from symbol ch i 10 d 1 100000 symbol index for d is 1 symbol index for unknown is 1 The total number of symbols is 7 symbol index 0 name i address 40a60 symbol index 1 name d address 57b00 symbol index 2 name prototype address 0 symbol index 3 name e address O0 symbol index 4 name s address 53628 symbol index 5 name func address 54cd0 symbol index 6 name main address 55180 Ch SymbolAddrByName interp main 55180 localvariable is not CH LOCALVARTYPE or CH GLOBALVARTYPE func is CH GLOBALVARTYPE Number of global variables of not function type is 4 Figure 2 2 Output of the executable program from symbol c The information about variables can be obtained by other functions The data type array element data type and function return type of a variable can be obtained by function Ch_DataType If a variable is an ar ray its array type dimension and extent for each dimension can be obtained by functions Ch ArrayDim Ch ArrayExtent and Ch ArrayType respectively Whether a variable is function function with vari able number of arguments and the number of arguments can be obtained by functions Ch FuncTypeO Ch IsFuncVarArg and Ch FuncArgNum respectively Ch FuncType can be used to test whether a symbol is function with definition funct
272. in a Header File in C Space from Ch Space with dl File Variables and data in C space can also be accessed from Ch space through a dynamically loaded library in a dl file which can be illustrated by Figure 4 2 Assume that we have a C program embedch3 exe in which there is a global variable c var This C program invokes a Ch program embedch3 ch with embedded Ch and then calls Ch function chfun by its name Our goal here is to access the C global variable c var from chfun The method we will use is to load a DLL file 1ibembedch3 d1 in which a chdl function returns the value of c var Note that the type specifier IMPORTCH defined in header file ch h in declaration of c var in embedch3 chdl c indicates it is an external variable and the same memory of c var in embededch3 exe is used The source code of this example is listed in Programs to 4 12 Besides the value of c var the Ch function chfun in Program 4 11 also obtains the address of C global variable c var by passing the address of ch_varptr which is declared as a pointer to int to chdl function embedch3 chdl Therefore the value of c var can be changed in Ch space and be effective after returning back to C space Note If the global variable in C space is a type of const we can obtain the value of global variable instead of address in Ch space The output from executing the file built from embedch3 c is shown in Figure 4 3 73 CHAPTER 4 ACCESSING C C VARIABLES FROM C
273. ining the address of a member of a class its value can be assigned in the C space directly as shown below void addr address for a member of a class int ptri ptri addr ptri 10 assign the value for a member of a class 161 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES File Name prog5 c include lt stdio h gt include lt string h gt include lt embedch h gt void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int main ChInterp t interp char argvv prog5 ch NULL int mask count 0 ChPointer_t clientdata NULL Ch Initialize amp interp NULL mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 void callback ChInterp t interp ChBlock_t calldata ChPointer_t clientdata int level 0 ChBlock t calldata2 char name 256 expr 256 int j double d if calldata gt event amp CH MASKEND amp amp strcmp calldata gt funcname funcl printCalldata calldata use scanf or fgets to obtain a variable name such as y from the user strcpy name y j 50 use scanf to obtain the value from the user if Ch VarType interp name amp amp Ch FuncType interp name setVariable interp name 4j
274. int int int int linecurrent int int linefunceng event count level linefuncbegin const char source const char funcname const char classname int isconstructor int isdestructor ChBlock t mask of callback event instructions for CH MASKCOUNT every i nt count level2 level1 int level level0 H P cho kh ct och ct boob ct o o o t o t leveO he current running function current func Level 0 is the f Qo un he 1 he he line n ine n he file n therwise class name NULL rue if it is a constructor of class fal therwise therwise NULL umber where t ir definition of function begins umber wh ne Ry re th definition of unction ends 2 ame if the source is a file t is a string haracters beginning with funci string Ey 6 therwise S rue if it is a destructor of class fal arg S o whereas level ntl ction that has called level n he current line num wher s executing T the given function the therwise It contains the first 70 tion name if the block is a function if it is a member function Type ChUserDefinedInfo t is a structure used to obtain the information for a user defined struct
275. int fun array int a n array int b array int c amp The macro CH UNDEFINETYPEhas the value 0 All others are non zero values Parameters interp A Ch interpreter name Name of the variable or expression in the Ch space Description The function Ch Array TypeO determines if the argument is array type Unlike function Ch FuncArgArrayType a poiner to C array is not considered as an array by function Ch ArrayType for the convenience of runtime manipulation of variables For example for the script code int p 2 3 The array type of p using function Ch_DataType is CH UNDEFINETYPE The data type of an array shall be determined by function Ch_DataType Example See Ch_DataType See Also Ch_DataType Ch_ArrayDim Ch_ArrayExtent Ch ArrayNum Ch_FuncType Ch FuncArgNum Ch IsFuncVarArg Ch UserDefinedTag Ch UserDefinedInfo 224 Chapter A APls for Embedding Ch lt embedch h gt Ch ChangeStack Ch ChangeStack Synopsis include lt ch h gt int Ch_ChangeStack ChInterp_t interp int level ChBlock_t block Purpose Change the stack for the current function of an executed Ch program Return Value If the function is successful it returns CH_OK If the specified stack level is invalid it returns CH_INVALIDLEVEL Otherwise it returns CH ERROR Parameters interp A Ch interpreter level The level of the function being executed block The block information for the specified leve
276. int i int a 2 int b 2 3 the information of number of dimensions and extents is obtained from the prototype of the function For the C function with arguments of variable length arrays such as int funct2 int i int a int b 3 int m int n the information of dimensions and extents is often obtained from the additional arguments for example arguments m and n for arrays a m and b n 3 in the function prototype funct 20 However in the argument list of a Ch function an array which can be either C array or Ch computational array is passed as assumed shape array more information about this array which is invisible to users is also passed through the argument list inexplicitly So for a Ch function users don t need to pass extra arguments explicitly except for the array name For example the prototype of Ch function with argument of VLA could be int funct3 int i int a int b 116 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE More information about the arrays in Ch can be found in Ch User s Guide Different methods of call ing Ch functions with arguments of VLAs in the Ch program using APIs Ch CallFuncByName and Ch CallFuncByAddr will be described in this chapter It is equivalent to build an argument list using API Ch VarArgsAddArg for function call of Ch CallFuncByNameVar 6 1 1 1 Calling Ch Functions with Argume
277. interface to Ch scripts is described with examples At the end a sample text based debugger is presented The same code has been used inside Ch IDE distributed in Ch Professional Student and Evaluation Editions 7 1 Obtaining Program and Function Information By a Callback Function Embedded Ch offers a user defined callback function that can be called during the program execution A callback function is added by the function int Ch AddCallback ChInterp t interp int event ChCallback t callback ChPointer t clientdata int count Only one callback function can be specified for an interpreter A subsequent function call of Ch AddCallback will overwrite the previously specified events callback function client data and count value described be low The first argument interp is the interpreter The second argument event specifies on which events the callback will be called The callback function may be called in seven different events each associated with an event mask as follows 1 Ch MASKCALL Just after Ch calls and enters a new function 2 Ch MASKRET Just before Ch leaves the function 3 Ch MASKBLOCK Just after Ch enters a new block 4 Ch MASKEND Just before Ch leaves the block 5 Ch MASKLINE When Ch is about to start the execution of a new line of code or when it jumps back in the code even to the same Ine 6 Ch MASKCOUNT After Ch executes every count instructions 7 Ch MASKABORT When Ch is aborted by Ch Abort
278. interp int func int i c_i 10 assign c_i in C space to ch_i in Ch Ch_SetVar interp ch_i CH_INTTYPE h ExprEval interp printf ch_i h ExprEval interp printf N ch p assign amp c i in C space to ch p in C h SetVar interp ch p CH INTPTRTYP call function func ExprEval interp End interp return 0 n Ci CY x ACS Aa n Output ch_i 10 ch_p 10 func ch i 50 See Also printf N func ch Xy hi hk i ch j ch k 20 ch p ch i ch p s ant uw23 r2 5 i return i2 space Giy dNnN y eh sd n ch_p h space amp c i E i dWMnV func ch i Ch DeclareFunc Ch DeclareTypedef Ch AppendParseScript Ch AppendRunScript 243 Chapter A APIs for Embedding Ch lt embedch h gt Ch DeleteExpr Value Ch DeleteExpr Value Synopsis include lt ch h gt int Ch DeleteExpr Value ChInterp_t interp ChValueNode_t vn Purpose Delete a value node for an expression in the Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter vn The value node of an expression to be deleted Description When an expression in the Ch space is evaluated by the function Ch ExprValue a value node for the result of the evaluated expression is returned to keep track of the temporary memory This memory s
279. interperter interp because it does not exist any more Ch End i nt erp can also not be invoked indirectly through any Ch SDK or Embedded Ch SDK API Ch_Abort int erp is equivalent to abort in C Ch Abort interp will abort the execution of the embedded Ch script upon the completion of the statement currently in execution For example a Ch function may call a binary C function which contains Ch Abort i nt erp call to abort the execution of the function This Ch function can be invoked by APIs such as Ch RunScript and Ch_CallFuncByName The resources for the interpreter shall be deleted by function call of Ch End i nt erp later Ch Abort i nt erp be called in a thread to abort execution of an embedded Ch script in another thread The execution of the script can be aborted by itself either in the Ch space using abort or Ch Abort in the binary C space in the same thread or a different thread Example 1 in Unix Abort the execution of a Ch script by Ch Abort from a different thread The interpreter handle interp is a global variable You may need to add the lib for pthread by the option Ipthread chabort thread1l c A C program aborting a Ch script from a different thread include lt pthread h gt include lt stdio h gt include lt stdlib h gt include lt embedch h gt char code int func while 1 printf hello n return 0 ChInterp t interp void thread functionl void arg int retval printf th
280. interpreter cleve The current stack level hleve The highest possible stack level at the point of execution Description Function Ch_StackLevel obtains the current stack level and highest possible stack level Level 0 is the currently executed function whereas level n 1 is the function that has called level n The program scope outside any function is the top level For example when function level0 is being called in program proc c it has level 0 Function Level1 which calls function level0 has level 1 Function level2 which calls function level1 in turn has level 2 Program prog c has the stack level 3 which is the highest possible level File name prog c void level2 int i2 20 levell void levell int il 10 level0 void levelO 10 1 this_part_of_the_function_level0_is_being_executed int main f level2 return 0 299 Chapter A APIs for Embedding Ch lt embedch h gt Ch StackLevel As an example a callback function is invoked when the code inside function 1evelO in the above code is executed The current stack level O and highest possible stack level 3 can be obtained by the code below int clevel hlevel ChStackLevel interp amp clevel amp hlevel clevel becomes 0 hlevel becomes 3 Example See Ch GlobalSymbolAddrByIndex See Also Ch ChangeStack Ch StackName 300 Chapter A API
281. ion prototype pointer to function member function constructor destructor of a class The symbol table contains global and local variables Function Ch VarType can be used to check if a symbol is a variable global variable or local variable as shown in Program 2 4 Function numOfVar returns the number of variables of not function type Program 2 3 has four variables i d e and s of not function type The tag name size and total number of members of a user defined class structure or union can be obtained by functions Ch UserDefinedInfo The information for a member of of a user defined class structure or union can be obtained by functions Ch UserDefinedMemInfoByIndex or Ch UserDefinedMemlInfoByIndex 2 2 Assigning Values in C Space to Variables in Ch Space Values in the C space can be assigned to variables in the Ch space by function Ch SetVar with the follow ing function prototype int Ch SetVar ChInterp t interp const char name ChType t atype For a simple data type the function will be called as if it was prototyped as follows int Ch SetVar ChInterp t interp const char name ChType t atype type value where name is an identifier in the Ch space atype is the data type ChType t of the value in the C space defined in header file CHHOME extern include ch h the last argument value is an expression or a variable in the C space It can be any valid data type in C such as int double pointer to int array stru
282. ive statement of the executed program is inside a function However it might be necessary to access variables in the calling function such as for a debugger For example when the program flow is at the end of the if statement block inside the active function funcl the user callback might want to access the local variables i and x in the calling function main To access variables in a calling function the function stack of the Ch program needs to be changed by the API Ch ChangeStack with the following function prototype int Ch ChangeStack ChInterp t interp int level ChBlock t calldata Function Ch ChangeStack changes the interpreter runtime function stack The second argument level specifies the level of the function being executed Level 0 is the current running function whereas level n 1 is the function that has called level n The stack will be changed to the level 0 internally when a callback function specified by Ch AddCallback returns However if the stack is changed not inside a callback function it needs to be reset to level 0 to resume the execution of the original running program The third argument of the function Ch ChangeStack passes back the information for the specified level 135 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK File Name prog2 c include lt stdio h gt include lt string h gt include lt embedch h gt void callback ChInterp t interp ChB
283. k CK kCk k Ck kc k kk k k k k k k ck k ck ck ck Ok kk ke kk File Name setvar cpp OKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCKCKCkCKCKCKCkCKCKCKCkCKCkCKCk Ck k ck k ck kk ck k kk kk ck kk k kk ke ke ke e Ke x include lt stdio h gt include lt embedch h gt struct tag ine double f s 10 20 s2 100 200 sa 4 10 20 1 2 3 4 5 6 class tage public int i double f Je 10 20 union tagu aint 1j double f ju int main ChInterp t interp int status char argvv setvar ch NULL int retval 0 int i 5 double d 10 const char cp string in main int n 2 m 3 int a 2 3 1 2 3 4 5 6 int b 3 1 2 3 u i 10 Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program setvar ch failed n Ch_SetVar interp i CH_INTTYPE 10 Ch_ExprEval interp printf i printed from setvar c d n i 296 Chapter A APIs for Embedding Ch lt embedch h gt Ch SetVar interp dp CH DOUBLEPTRTYPE amp d Ch SetVar interp chp CH CHARPTRTYPE cp Ch SetVar interp a CH CARRAYTYPE CH INTTYPE a 2 n m Ch SetVar interp b CH INTPTRTYPE b Ch SetVar interp a2 CH CHARRAYTYPE CH INTTYPE a 2 n m Ch SetV
284. l dClassl chdl void varg ChInterp t interp ChVaList t ap class Classl c Ch VaStart interp ap varg c Ch VaArg interp ap class Classl if Ch CppIsArrayElement interp c Glassli else delete c Ch VaEnd interp ap return EXPORTCH int Classl memfunl chdl void varg ChInterp t interp ChVaList t ap class Classl c int i int retval Ch VaStart interp ap varg c Ch VaArg interp ap class Classl i Ch VaArg interp ap int get 1st arg retval c memfunl i Ch VaEnd interp ap return retval Program 5 31 chdl file sampclass_chdl cpp 103 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE File prog cpp include lt stdio h gt include lt embedch h gt include sampclass h int main ChInterp t interp char argvv script cpp NULL int status class Classl cl printf Begin output from prog exe n cl memfunl 10 printf End output from prog exe n initialize embedded Ch Ch Initialize amp interp NULL run a Ch program status Ch RunScript interp argvv if status printf Error execution of script cpp failed Mn Ch End interp return 0 Program 5 32 C program invoking embedded Ch programs prog cpp target prog exe prog exe prog o sampclass o sampclass chdl o ch dllink prog exe cplusplus embedded pr
285. l is 0 The current line number is 21 Function main starts at line 17 Function main ends at line 22 The function name is main The current file name is progl ch i 10 Figure 7 1 Output from executing progl c 134 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK function for each event Note that variable i is used as a global variable argument for function funcl and local variable inside function main and if statement block inside function unc1 The address of the variable i is obtained by the function Ch_SymbolAddrByName as follows p int Ch SymbolAddrByName interp i and its value is printed in the function callback Function Ch SymbolAddrByName searches the symbol i in the symbol tables within its scope The call event for function main will trigger the callback function before the local variable i is initialized to 10 by the initialization statement Note that a variable is by default initialized to O when its memory is allocated in Ch The local variable i with the value 0 is processed by the callback function The output from the callback function when it is first called due to the call event for function main appears in the first block in Figure 7 1 The line sp gt i 10 sp gt d 20 000000 displays the client data passed from the fourth argument of the function Ch AddCallback when it was called The second time when the function call
286. l of function Description Function Ch_ChangeStack changes the interpreter runtime function stack The second argument level specifies the level of the function being executed Level 0 is the currently executed function whereas level n 1 is the function that has called level n For example when function level0 is being called it has level 0 Function levell which calls function 1evel0 has level 1 Function Jevel2 which calls function 1evell in turn has level 2 void level2 int i2 levell 20 void levell int il 10 level0 void levelO i0 1 this part of the function level0 is being executed As an example if a callback function is invoked when the code inside function 1eve10 in the above code is executed Other API functions can be used to access and change the value of the variable i 0 inside function 1evelO To access the value for the variable i1 inside the calling function 1evel1 The stack of the current function needs to be changed as follows ChBlock t block level 1 ChChangeStack interp level amp block 225 Chapter A APIs for Embedding Ch lt embedch h gt Ch ChangeStack The information for the current function 1evell after changing the stack is passed back to the third argument block of ChBlock t type which is a structure containing the information for a block It is described in detail in the reference for the API Ch AddCal
287. laced under the home directory of your application such as C myApplication embedch Your application program should set the Embedded Ch home directory properly as shown in example in Program I 5 A sample code for distribution of Embedded Ch without regular Ch is given in Program IA To distribute your application with Embedded Ch but without regular Ch follow the steps below 1 If your application is distributed in the directory C myApplication in a target machine Copy and distribute the contents in the directory CHHOME toolkit embedch of the Embedded Ch in your development machine as C myApplication embedch in the target machine 2 In Windows make sure applications will be able to access file ch d11 In general the dynami cally linked library ch d11 for Embedded Ch shall be located at the same directory which con tains the hosting application with Embedded Ch For example if the binary application is located in C myApplication bin copy CHHOME toolkit embedch bin ch dll to C myApplication bin ch dll If the binary hosting application with Embedded Ch is lo cated in the Windows system directory such as C Windows System32 it is not recommended to put ch dll over there Instead the environment variable PATH can be set with the directory C myApplication bin which contains ch d11 This will reduce the chance of the conflict since the installation of regular standardalone Ch puts ch dll in the Windows system directory 3 The secon
288. lar function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is the function name for Ch CallFuncByName The third argument is the pointer containing the address of the return value of the called function If the Ch function takes arguments they 51 CHAPTER 3 ALONG UNG ON ETON PNS CISBMIEUSING CH CALLFUNCBYNAMEO AND CH CALLFUNCB YNAMEV KK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk k k k k k Ck k k k k ck k ck kk k ck ck ck ok ko OK File Name funcall3 c Calling Ch functions by evaluating expressions OKCKCKCKCKCkCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCk CK KCKCkCKCKCKCKCKCkCKCKCKCkCkCkCKCk Ck k k ck k k k kk k ok ko ke ke ex include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv funcall2 ch NULL void fptr int retval_g initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program funcall ch failed in Ch CallFuncByName interp func amp retval g 10 call Ch function func printf In C after the first function call Mn printf retval g d n retval g Ch ExprEval interp ii Ch ExprEval interp dd dd ii Ch CallFuncByName interp
289. lass1 Class1 a destructor Classl Classl and a member function Class1 memfunl1 in C space The definitions for the constructor destructor and member function are defined in Program 5 30 Member functions can be exported for callback from Ch scripts Head file sampclass h in Pro gram 5 29 is shared by both Ch and C The predefined macro CH is used to distinguish the code block used for these two different execution and compilation environments This program is compiled and linked with Program 5 32 for program prog cpp to form an executable binary program Class file sampclass cpp in Program 5 30 interface functions sampclass chdl cpp in Pro gram 5 31 and application program program prog cpp in Program with C header file sampclass h in Program 5 29 are compiled and linked to create an executable program prog exe A Makefile program Program 5 33 and 5 34 can be used to create the executable program prog exe used in this example for Unix and Windows respectively 100 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE ifndef SAMPCLASS H define SAMPCLASS H KR KKK KR KK for Ch space KOK OK OK OK x XI x ifdef _CH_ include lt dlfcn h gt ifdef _HPUX_ void g_sample_dlhandle else extern void g sample dlhandle dlopen NULL RTLD LAZY if g sample dlhandle NULL fprintf stderr Error dlopen s n dlerror
290. lasses in the main application program These C functions or C classes can be either stati cally linked to or dynamically loaded for the main application Section 5 1 illustrates how to call C func tions which are part of the main application program The source code including Visual NET project file embedfuncmain vcproj and VC project files embedfuncmain dsw and embedfuncmain dsp for the example described in section can be found in in the directory CHHOME toolkit demos embedch chapters chapter5 embedfuncmain File CHHOME toolkit demos embedch chapters chapter5 embedfuncmain readme has more detailed information about setup for compiling programs in Visual NET and VC Treating a binary C function as a system function in the Ch space section 5 2 illustrates how to call C functions even without using function files from a Ch program The source code for this section is available in the directory CHHOME toolkit demos embedch chapters chapter5 declarefunc Section B 3 shows how to call back a binary C function inside an application from the Ch space The source code for this section is available in the directory CHHOME toolkit demos embedch chapters chapter5 callback Section 5 4Jillustrates calling C functions from Ch space using a dynamically loaded library 1ibsample dl file Invoking C classes in the main application program from a Ch script without a separate d1 file is illustrated in section 5 The source code including Visual NET and VC
291. lback In the above example after the function call the value of block level for the member level of block is 1 The member linecurrent of block is 0 at the top level The stack will be changed to the level 0 internally when a callback function specified by Ch AddCallback returns However if the stack is changed not inside a callback function it needs to be reset to level 0 to resume the execution of the original running program as shown below ChBlock t block ChChangeStack interp 0 amp block Example See Program 7 3 See Also Ch AddCallback Ch StackLevel Ch StackName 226 Chapter A APIs for Embedding Ch lt embedch h gt Ch Close Ch Close Synopsis include lt embedch h gt int Ch Close ChInterp t interp ChFile t filedes Purpose Close a file descriptor Return Value Upon successful completion Ch Close returns 0 Otherwise it returns EOF Parameters interp A Ch interpreter fildes The file descriptor for the stream to be closed Description The Ch_Close function calls fclose to flush the stream and close the file associated with the file descrip tor fi ledes returned from function Ch Reopen Example Refer to Program See Also Ch Reopen Ch Flush 227 Chapter A APIs for Embedding Ch lt embedch h gt Ch DataSize Ch DataSize Synopsis include lt ch h gt size_t Ch_DataSize ChInterp_t interp const char name Purpose Get the size of a local or gl
292. le sum2d double a function to be called from C space int i j double retval 0 array int ext 2 shape a printf ext 0 d n ext 0 printf ext 1 d n ext 1 for i 0 i lt ext 0 i for j 0 j lt ext 1 j retval a i jl return retval Program 6 1 Example of calling Ch functions with arguments of assumed shape arrays callchvla2d ch three extra arguments dim ext 1 and ext 2 which contain the dimension and extents of aa1 are passed to Ch function For array aa2 its dimension and extents are also passed as extra arguments in the function call below dim 2 extl 3 ext2 4 dim and extents of aa2 Ch CallFuncByName interp sum2d sum ret aa2 extl ext2 Note that the dimensions of arrays in C program such as aa1 and aa2 should be the same as the dimensions of the array argument the Ch function sum2d In this example sum2d takes a 2 dimensional array as the argument arrays in C space should also be two dimensions The output from executing this example is shown in Figure 6 1 If the Ch function takes the argument of an array of reference without constraint of dimension arrays with different dimensions can be passed to it and the information of dimensions needs to be passed This special case will be discussed in the next section 6 1 1 2 Calling Ch Functions with Arguments of Arrays of Reference In this section we will describe how to call Ch functi
293. les in C Space from Ch space embedch3 chdl c 76 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 3 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE WITH DL FILE in embedch3 c amp c var 20d58 c var 10 in embedch3 chdl c amp c var 20d58 c var 10 in the Ch function ch var 10 in embedch3 c amp c var 20d58 c var 20 in embedch3 c amp c var 20d58 c var 100 in embedch3 chdl c amp c var 20d58 c var 100 in the Ch function ch var 100 in embedch3 c amp c var 20d58 c var 110 Figure 4 3 Output from executing embedch3 c TI Chapter 5 Calling C C Functions from Ch Space At the beginning of this document Programs I 1Jillustrates how an embedded Ch program can be invoked in a C application program The Ch program can in turn call functions in C space This chapter describes methods of how to call C functions and invoke C class from a Ch program Ch SDK User s Guide described the details on how to interface C functions and C classes in static or dynamical libraries in C space from Ch space when the starting program is a Ch program If the C C libraries are not part of the main application program please refer to Ch SDK User s Guide You will need to create a separate dynamically loaded library such as 1ibsample d1 for interfacing with C C library However no separate dynamically loaded library dl file is needed for a Ch script to call C functions or C c
294. ll be parsed then executed The process is similar to compile and execution using a C compiler Like Ch RunScript0 Ch AppendRunScriptFile performs these two steps together for a program or functions passed through its argument The function Ch AppendRunScriptFile executes a fragment of Ch program from C space The variable filename contains the file name of the Ch code The restrictions of Ch AppendRunScript do not apply to Ch AppendRunScriptFile See Also Ch AppendParseScript Ch AppendParseScriptFile Ch AppendRunScript Ch RunScript Ch ParseScript Ch ExecScript 220 Chapter A APIs for Embedding Ch lt embedch h gt Ch ArrayDim Ch ArrayDim Synopsis include lt ch h gt int Ch_ArrayDim ChInterp_t interp const char name Purpose Obtain the dimension of a local or global variable of array type in the Ch space if it is array type Return Value If the local or global variable is of array type This function returns the dimension of the array Otherwise it returns 0 Parameters interp A Ch interpreter name Name of the variable or expression in the Ch space Description This function returns the dimension of a local or global variable in the Ch space The extent of each dimen sion of the array can be obtained by function Ch_ArrayExtent Example See Ch_DataType See Also Ch DataTypeO Ch ArrayExtent Ch ArrayNum Ch ArrayType Ch_FuncType Ch FuncArgNum Ch IsFuncVarAr
295. llFuncByName interp sum2d amp sum ret aa2 dim extl ext2 printf sum2d has been called from C space and returned f n sum ret Ch End interp Program 6 2 Example of calling Ch functions with arguments of assumed shape arrays callchvla2d c ext 0 2 ext 1 3 sum2d has been called from C space and returned 21 000000 ext 0 3 ext 1 4 sum2d has been called from C space and returned 78 000000 Figure 6 1 Output from executing callchvla2d exe 119 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt array h gt double sum2d array double a amp amp function to be called from C space double retval 0 int i j array int ext 2 shape a int n ext 0 m ext 1 array double aa n m aa a printf ext 0 d n ext 0 printf ext 1 d n ext 1 for i 0 i lt n i for j 0 j lt m j retval aa i jl return retval Program 6 3 Example of calling Ch functions with arguments of array of reference with fixed dimension callchvla2d ch The way of calling Ch function with arguments of array of reference with fixed dimension is exactly the same as calling Ch function with arguments of assumed shape array which has been described in the previous section The extra arguments providing information of extents of arrays need to be passed to the Ch function
296. llFuncByNameVar shall be called to exe cute the code This implies that only appended functions can be executed The parsed code by Ch AppendParseScript can not be invoked by again later by function by Ch AppendRunScript Restrictions 1 The code passed can process the preprocessing directive include in the code Some preprocessing directives such as define have to be handled separately from other code For example Ch AppendParseScript interp define A 2 All preprocessing directives can exist in the code passed by function Ch RunScript Ch ParseScript Ch AppendRunScriptFile or Ch AppendParseScriptFile 2 The code passed cannot contain comments delimitered by such as this is comment If the code passed is larger than 5120 bytes Ch AppendParseScript saves it in a temporary file internally first then calls the API Ch AppendParseScriptFile to execute the file and finally removes the tempoary file is removed Alternatively if the code is larger than 5120 bytes it can be broken into multiple segments The user can then call the function multiple times to load these segments of code as shown below Ch AppendParseScript interp codel Ch AppendParseScript interp code2 215 Chapter A APIs for Embedding Ch lt embedch h gt Ch AppendParseScript This allows the code to run from the memory and is more efficient Example Refer to Program 1 7 See Also Ch AppendRunScript Ch RunScript
297. llo n return 0 func void thread_functionl void arg ChInterp t interp int retval interp ChInterp t arg printf thread_function called2 end n if Ch AppendRunScript interp code CH ERROR printf Error Ch AppendRunScript failed in thread function in printf thread_function called3 end n if Ch_CallFuncByName interp func amp retval CH_ERROR printf Error Ch CallFuncByName failed in thread_function n printf thread_function called4 end n return NULL 207 Chapter A APls for Embedding Ch lt embedch h gt Ch Abort void thread function2 void arg ChInterp t interp interp ChInterp t arg sleep 1 Ch Abort interp about func called by Ch AppendRunScript sleep 1 Ch Abort interp about func called by Ch CallFuncByName return NULL int main pthread t id 2 ChInterp t interp printf main called Win if Ch Initialize amp interp NULL CH ERROR printf Error Ch Initialize failed in main n pthread_create amp id 0 NULL thread_functionl interp pthread_create amp id 1 NULL thread function2 interp pthread join id 0 NULL pthread join id 1 NULL Ch End interp return 0 Example 3 in Windows Abort the execution of a Ch script by Ch_Abort from a different thread The interpreter handle interpis a
298. local variable This program chabort_thread2_win c for Windows is similar to chabort_thread2 c for Unix The Windows equivalent version of chabort threadl c is available in the distribution as chabort threadl win c chabort_thread2_win c A C program aborting a Ch script from a different thread in Windows include lt windows h gt include lt stdio h gt include lt stdlib h gt include lt embedch h gt define N 2 char code int func while 1 printf hello n return 0 func DWORD WINAPI thread_functionl PVOID arg int retval ChInterp t interp interp ChInterp t arg printf thread function called2 end n if Ch AppendRunScript interp code CH ERROR printf Error Ch AppendRunScript failed in thread function n printf thread function called3 end n if Ch CallFuncByName interp func amp retval CH ERROR printf Error Ch CallFuncByName failed in thread function Nn printf thread_function called4 end n return 100 208 Chapter A APls for Embedding Ch lt embedch h gt Ch Abort DWORD WINAPI thread function2 PVOID arg ChInterp t interp interp ChInterp t arg _sleep 1000 Ch Abort interp about func called by Ch AppendRunScript _sleep 1000 Ch Abort interp about func called by Ch CallFuncByName return 200 int main ChInterp t interp HANDLE a thread N DWORD a threadId N DWOR
299. lock t calldata ChPointer_t clientdata int main int argc char argv ChInterp t interp char argvv int mask count 0 ChPointer t clientdata NULL if argc 2 printf Usage s chscriptname n argv 0 exit EXIT FAILURE create char argvv chscriptname NULL dynamically argvv char malloc 2 sizeof char argvv 0 argv 1 argvv 1 NULL Ch Initialize amp interp NULL mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 Program 7 3 C program which changes the function stack prog2 c 136 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int p level 0 ChBlock t calldata2 if calldata gt event amp CH MASKEND amp amp strcmp calldata funcname funcl printCalldata calldata symbols interp while Ch ChangeStack interp level amp calldata2 CH OK printCalldata amp calldata2 symbols interp int symbols ChInterp t interp int index totalnum p char name totalnum Ch SymbolTotalNum interp printf The total number of symbols is d n totalnum for index 0 index lt totalnum index printf symbol index d name s address p n index Ch_SymbolNameByInde
300. low of the Ch program is outside a function the symbol table contains global variables The index number for the symbol table starts with 0 Example See Programs 2 4 and See Also Ch_SymbolTotalNum Ch_SymbolIndexByName Ch_SymbolAddrByName Ch_SymbolAddrByIndex 307 Chapter A APIs for Embedding Ch lt embedch h gt Ch SymbolTotalNum Ch SymbolTotalNum Synopsis include lt ch h gt int Ch_SymbolTotalNum ChInterp_t interp Purpose Get the total number of variables in the symbol table for variables and arguments of a function or in the symbol table for global variables in a Ch program Return Value The total number of variables in the symbol table in a Ch program Parameters interp A Ch interpreter Description The function Ch SymbolTotalNum obtains the total number of variables in the symbol table inside a dynamically loaded library or embedded Ch program If the control flow of the Ch program is inside a function the symbol table contains local variables including function arguments within its scope inside the function If the control flow of the Ch program is inside a member function of a class the symbol table contains local variables including members of the class and function arguments within its scope inside the function If the control flow of the Ch program is outside a function the symbol table contains global variables The symbol table contains user defined functions but no generic functions It
301. lows _ipath stradd ipath C myApplication ch include lpath stradd lpath C myApplication ch dl 6 Finally your application must include the following language in the copyright section or in other suitable place such as the About box of the product Contains Ch an embeddable C C interpreter developed by SoftIntegration Inc http www softintegration com All Rights Reserved 27 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 7 DISTRIBUTING EMBEDDED CH IN YOUR APPLICATIONS 1 7 1 Minimizing the Distribution of Runtime Components For many applications only a subset of Ch is needed In this case some of components in Embedded Ch can be removed in your distribution More specifically the following modules are removable e If your application is single thread and needs only one instance of the interpreter remove all chmt dl files except chmt 1 dl in the directory CHHOME toolkit embedch extern lib e Unix utilities such as ls exe grep exe sed exe etc for Windows in the directory option home toolkit embedch bin e OpenGL GTK Win32 X11 Motif toolkits in the directory option home toolkit embedch toolkit For applications using only expressions and built in mathematical functions listed in the header file math h in Embedded Ch files in option home toolkit embedch are not needed In this case however scripts processed by Embedded Ch shall not contain function files and header files The mem
302. lse bp gt condexpr NULL bp gt condvalue NULL bp gt condtrue condtrue bp gt next NULL if temp NULL no element in the linked list dgptr gt bkpt bp else get the tail of the linked list while temp gt next temp temp gt next add the new one to th nd of the list temp gt next bp return 0 int chSetBreakPointVar debugInfo t dgptr char varname char condexpr int condtrue breakPoint_t temp dgptr gt bkpt breakPoint_t bp breakPoint_t malloc sizeof struct BreakPoint bp gt btype ACTION SETBREAKPOINTVAR bp gt u var varname strdup varname bp gt u var varvalue NULL if condexpr 44 strlen condexpr 0 bp gt condexpr strdup condexpr else bp gt condexpr NULL bp gt condvalue NULL bp gt condtrue condtrue bp gt next NULL if temp NULL no element in the linked list dgptr gt bkpt bp else get the tail of the linked list while temp next temp temp gt next add the new one to th nd of the list temp gt next bp return 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 186 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chClearBreakPointLine debugInfo t dgptr char filename int linenum breakPoint t tmpl tmp2 tmpl dgptr gt bkpt if
303. lt from funcal13 c is the same as Figure 3 2 The argument of function func in Program is int type If an argument of the Ch function is a pointer type the data in the C space can be shared by the variable in the Ch space The details about sharing C space data by variables in the Ch space through function arguments are described in detail in section 1 A Ch function in a Ch program can also be called by its name with API Ch CallFuncByNamev The prototype of API Ch CallFuncByNamev is given in header file ch h as follows int Ch CallFuncByNamev ChInterp t interp const char name fptr void retval va list ap where ap is the variable argument list of a function obtained by va start This API can be used to implement user s own API for calling functions in the C C script space as shown Program The output from Program 3 6 is given as follows retval 5 000000 retval 20 000000 Details about Ch CallFuncByNamev can be found in Ch SDK User s Guide 53 CHAPTER 3 GALCNG ONCFON CURSING PROM ISAAOGEUSING CH CALLFUNCB YNAME AND CH CALLFUNCBYNAMEVO File callfuncbynamev c include lt stdio h gt include lt embedch h gt ChInterp t interp this function Some API is exported and documented for user int Some API char funcname void retval va list ap va start ap retval call Ch function by its name Ch CallFuncByNamev interp funcname retval ap va end ap int main char
304. mbedch2 ch are executed by calling Ch RunScriptM twice Then Ch program embedch2 ch again is parsed by Ch ParseScript and executed by Ch ExecScriptM Finally the Ch interpreter is terminated by function Ch End After a Ch program is executed by either Ch RunScriptM or Ch ExecScriptMO its global variables and functions shall not be accessed by other APIs 15 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS KK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kok kok k Ck k k ck k ck k ck k kok K k File Name embedchprograms c executing multiple Ch programs in a C program with embedded Ch COKCKCKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCkCKCkCk k ck k k k k k kk k k k kk include lt embedch h gt include lt stdio h gt include lt string h gt int main ChInterp t interp int status char argvl embedchl ch NULL char argv2 embedch2 ch opt1 opt2 NULL initialize embedded Ch Ch Initialize amp interp NULL execute embedchl ch status Ch RunScriptM interp argv1 f status CH ERROR printf Error execution of program embedchl ch failed n execute embedch2 ch status Ch RunScriptM interp argv2 if status CH ERROR printf Error execution of program embedch2 ch failed n execute embedch2 ch F status Ch ParseScript interp argv2 if
305. mber of function prototypes defined in the header file st dio h are suppressed in function chPrintSymbolValue Functions dlclose exit ad dlclose libstdio and global variable Chstdio handle are used to handle dynamically loaded library for functions in defined in the header file stdio h Like the variable pt di of pointer to int the value for the variable pt rd of pointer to C array is also the address it points to All elements of arrays a b vl v2 are displayed An array of char is treated as a string The values of arrays st r1 and st x2 are displayed similar as strings for variables st x3 and str4 For the variable s of structure and sa of array of structure the values for each member are also displayed Like Program 7 3 Program 7 8 can also process a script using C classes The output from executing the command prog4 cplus cpp for the script in Program 7 4 is shown in Figure 7 7 The output in Figure 7 7 is similar to one in Figure 7 3 except that instead of the addresses the values of each variable are displayed Similarly when Program 7 8 is used to process Program 7 5 with a nested function The output from executing the command prog4 nestedfunc ch is shown in Figure The function func1 in the stack level 1 is displayed as a local function in Figure 7 8 For comparison the output in Figure 7 8 is similar to one in Figure 7 4 except that instead of the addresses the values of each variable are displayed
306. minfo dtype CH DOUBLETYPE 1 1 meminfo ispublic meminfo isfunc sp gt i 100 sp gt f 200 000000 Ch DataSize interp s 16 struct size of sp is 16 struct name of sp is tag sp2 gt i 100 sp2 gt f 200 000000 234 Ch DataType Chapter A APIs for Embedding Ch lt embedch h gt udinfo dtype CH CLASSTYPE 1 class size of c is 16 class name of c is tagc union size of u is 8 union name of u is tagu Sa is struct type struct size of sa is 16 struct name of sa is tag Sa is an array sp gt i 10 sp gt f 20 000000 sp gt i 30 sp gt f 40 000000 b is a value of int b is a Ch computational array Array dim of b is 2 Array elements of b is 6 Extent of the 1st dim of b Extent of the 2nd dim of b bp 1 bp is a value of int b2 is a Ch VLA array Array dim of b2 is 2 Array elements of b2 is 200 Extent of the 1st dim of b2 10 Extent of the 2nd dim of b2 20 arrayfunc return type is double arrayfunc is Ch array Array dim of arrayfunc is 1 Array elements of arrayfunc is 3 ll N ll W Extent of the 1st dim of arrayfunc 3 arrayfunc is a function umber of arguments of function arrayfunc is 0 arrayfunc2 return type is double arrayfunc2 is Ch VLA array Array dim of arrayfunc2 is 2 Extent of the lst dim of arrayfunc2 is determined at runtime Extent of the 2nd dim of arrayfunc2 is determined at runtime arrayfunc2 is a function umber
307. mp strcmp bp u file filename calldata gt source 86 bp gt u file linenum calldata gt linecurrent check the conditional expr for each breakpoint if it exists if chTestBreakPointCondition interp bp 0 chProcessWatchExprs dgptr chProcessActionMenu dgptr return else if bp gt btype ACTION SETBREAKPOINTFUNC if calldata gt funcname NULL amp amp bp gt u func called FALSE amp amp first time into this function strcmp bp u func funcname calldata gt funcname if chTestBreakPointCondition interp bp 0 bp gt u func called TRUE chProcessWatchExprs dgptr chProcessActionMenu dgptr return else if bp gt btype ACTION SETBREAKPOINTVAR varptr void Ch SymbolAddrByName interp bp gt u var varname if varptr size Ch DataSize interp bp gt u var varname if bp gt u var varvalue NULL bp gt u var varvalue void malloc size save the value for the variable memcpy bp gt u var varvalue varptr size else if memcmp bp gt u var varvalue varptr size if chTestBreakPointCondition interp bp save the value for the variable memcpy bp gt u var varvalue varptr size chProcessWatchExprs dgptr chProcessActionMenu dgptr return 0 bp bp gt next Program 7 17 The callback for the debug program callback c Contd 197 Chapter APIs for Embedding Ch lt embedch h gt
308. mpl gt exprname if tmpl exprvalue free tmpl exprvalue dgptr gt wexpr NULL return 0 int chSetBreakPointLine debugInfo t dgptr char filename int linenum char condexpr int condtrue breakPoint t temp dgptr gt bkpt breakPoint t bp breakPoint t malloc sizeof struct BreakPoint bp gt btype ACTION SETBREAKPOINTLINE bp gt u file filename strdup filename bp gt u file linenum linenum if condexpr 64 strlen condexpr 0 bp gt condexpr strdup condexpr else bp gt condexpr NULL bp gt condvalue NULL bp gt condtrue condtrue bp gt next NULL if temp NULL no element in the linked list dgptr gt bkpt bp else get the tail of the linked list while temp next temp temp gt next add the new one to the end of the list temp gt next bp return 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 185 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chSetBreakPointFunc debugInfo t dgptr char funcname char condexpr int condtrue breakPoint_t temp dgptr gt bkpt breakPoint_t bp breakPoint_t malloc sizeof struct BreakPoint bp gt btype ACTION_SETBREAKPOINTFUNC bp gt u func funcname strdup funcname bp gt u func called FALSE if condexpr 44 strlen condexpr 0 bp gt condexpr strdup condexpr e
309. n Ch ArrayDim interp arrayfunc printf Array elements of arrayfunc is d n Ch ArrayNum interp arrayfunc printf Extent of the lst dim of arrayfunc d n Ch ArrayExtent interp arrayfunc 0 if Ch_FuncType interp arrayfunc printf arrayfunc is a function n printf Number of arguments of function arrayfunc is d n Ch FuncArgNum interp arrayfunc if Ch_DataType interp arrayfunc2 CH DOUBLETYPE printf arrayfunc2 return type is double n if Ch_ArrayType interp arrayfunc2 CH CHARRAYVLATYPE printf arrayfunc2 is Ch VLA array n printf Array dim of arrayfunc2 is d n Ch ArrayDim interp arrayfunc2 if Ch ArrayExtent interp arrayfunc2 0 INT MAX printf Extent of the 1st dim of arrayfunc2 is determined at runtime Wn if Ch ArrayExtent interp arrayfunc2 1 INT MAX printf Extent of the 2nd dim of arrayfunc2 is determined at runtime Wn if Ch_FuncType interp arrayfunc2 printf arrayfunc2 is a function n printf Number of arguments of function arrayfunc2 is d n Ch_FuncArgNum interp arrayfunc2 Ch End interp varinfo ch A Ch program include lt array h gt int n 10 double d 1 1 int a 2 10 20 a2 n pa 3 struct tag Ai xf double f l class tage public int dui double f union tagu Amt IT double f struct tag s 100 200 sp sa 2
310. n 1 arg 2 of 7 is function 1 arg 3 of 7 is function 1 arg 4 of f7 is function 1 arg 1 of f8 is function 1 arg 2 of f8 is function 1 arg 3 of f8 is function 1 arg 1 of fl is variable arg 0 arg 4 of fl is variable arg 0 arg 1 of f7 is variable arg 0 arg 2 of f7 is variable arg 0 arg 3 of f7 is variable arg 0 267 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgDataType arg 4 of f7 is variable arg 1 arg 1 of f8 is variable arg 0 arg 2 of f8 is variable arg 0 arg 3 of f8 is variable arg 0 func arg num for arg 1 of fl is 1 func arg num for arg 4 of fl is 1 func arg num for arg 1 of f7 is 1 func arg num for arg 2 of f7 is 0 func arg num for arg 3 of f7 is 3 func arg num for arg 4 of f7 is 1 func arg num for arg 1 of f8 is 1 func arg num for arg 2 of f8 is 1 func arg num for arg 3 of f8 is 1 data size for arg of fl is 1 data size for arg Of Tas data size for arg of 6 Xs l6 data size for arg of f6 is 16 data size for arg of f6 is 16 data size for arg of f6 is 16 data size for arg of f6 is 8 data size for arg of f6 is 8 data size for arg of f6 is 16 data size for arg of f6 is 16 data size for arg of f6 is 16 ta name for arg of Xl 155 0 data name for arg of fl is 0 o ta name for arg ta name for arg ta name for arg ta name for arg ta name for arg ta name for arg ta name for arg ta name for arg ta name for arg of f6 is tag of f6 is tag of f6 is tagc of f6 is tagc of f6 is tagu of f6 is tagu
311. nc2 amp retval ap ch Ch VarArgsAddArg amp ap ch CH DOUBLETYPE d Ch CallFuncByNameVar interp func2 amp retval ap ch Ch VarArgsDelete interp ap ch 61 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS include lt stdarg h gt int funcl va list ap int vacount i val i float val f double val d vacount va count ap printf vacount din vacount for i 0 i lt vacount i if va_elementtype ap elementtype int val_i va_arg ap int printf val_i d n val i else if va_elementtype ap elementtype float val_f va oo float printf ty Sf n val f else if va_elementtype ap elementtype double val_d va ur double printf v Sf n val d return 0 int Lunes int 1 2 A int vacount val_i i float val_f double val_d va_list ap va_start ap i vacount va_count ap printf vacount d n vacount for i 0 i lt vacount i if va_elementtype ap elementtype int val_i va_arg ap int printf val_i d n val i else if va elementtype ap elementtype float val f va ET float printf v f n val f else if a_elementtype ap elementtype double v val d va eae double a f n val 3 printf va end ap return 0 int funes ins 1 int vacount i va list ap va s
312. nd dWMn il i2 return Program 4 4 The chdl functions for handling global variables expvar chdl c 69 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 2 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE include lt stdio h gt include lt embedch h gt int main ChInterp t interp char argvv expvar ch NULL int status initialize embedded Ch Ch Initialize amp interp NULL run a Ch program status Ch RunScript interp argvv if status printf Error execution of program ch failed n Ch End interp Program 4 5 C program invoking embedded Ch program prog c target prog exe prog exe prog o expvar chdl o ch dllink prog exe embedded prog o expvar chdl o prog o prog c ch dlcomp libsample dl prog c expvar chdl o expvar chdl c expvar c h ch dlcomp libsample dl expvar chdl c I clean rm f o obj exp lib exe Program 4 6 Makefile for Unix Makefile target prog exe prog exe prog obj expvar chdl obj ch dllink prog exe embedded prog obj expvar chdl obj prog obj prog c ch dlcomp libsample dl prog c expvar chdl obj expvar chdl c expvar c h ch dlcomp libsample dl expvar chdl c I clean rm f o obj exp lib exe Program 4 7 Makefile for Windows Makefile As described in the previous chapters function dlopen is used to load the application program itself for accessing its exported variables in the Ch heade
313. ndString ChVaList_t arglist initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv f status CH ERROR printf Error execution of program callchvna ch failed n F printf calling func from C space Mn Ch CallFuncByName interp func amp retvalp strl printf retvalp s n retvalp free retvalp string t func string t printf calling func from C space Mn arglist Ch VarArgsCreate interp Ch VarArgsAddArg interp amp arglist CH CHARPTRTYPE str2 Ch CallFuncByNameVar interp func amp retvalp arglist Ch VarArgsDelete interp arglist printf retvalp s n retvalp free retvalp Ch End interp Program 6 9 A C program calling Ch functions that string type string t callstring c calling func from C space s in func firstString retvalp firstStringABCD calling func from C space s in func secondString retvalp secondStringABCD Figure 6 4 Output from executing callstring c 127 Chapter 7 The Debug and Callback Interface to a Ch Program Ch has limited built in debugging facilities But Embedded Ch offers debug interfaces by means of a set of interface APIs These interfaces allow the construction of different kinds of debuggers profilers IDEs and other tools that can access and modify active Ch scripts In this chapter the debug and callback
314. nded with the type qualifier declspec global For example the declaration Ch DeclareFunc interp double func int i funcptr is treated as if the the function protpotype was declared as declspec global double func int i A new data type for return value and arguments of a system function can be created using the API Ch DeclareTypedef Both global functions and system functions can be removed by a pragma statement as shown below pragma remvar func in the Ch space which can accomplished using Embedded Ch by Ch AppendParseScript interp fpragma remvar func The same identifier cannot be declared as a variable for both global function and system function with function definitions for both A struct type used as a function argument or return type of a system function defined by function Ch DeclareFunc can be declared by function Ch DeclareVar To call binary functions from an ap plication with Embedded Ch The application typically defines binary functions as system functions in the Ch space using functions Ch DeclareVar and Ch DeclareFunc Then run Ch programs by 236 Chapter A APIs for Embedding Ch lt embedch h gt Ch DeclareFunc functions Ch ParseScriptO Ch RunScriptO Ch RunScriptM as well as Ch AppendParseScript Ch AppendParseScriptFile Ch AppendRunScript Ch AppendRunScriptFile When multiple Ch programs are executed by function Ch RunScriptM in a single instance of Embedded Ch the sam
315. ndex obtains the address of the variable inside a dynamically loaded library or embedded Ch program If the control flow of the Ch program is inside a function the symbol table contains local variables including function arguments within its scope inside the function If the control flow of the Ch program is inside a member function of a class the symbol table contains local variables including members of the class and function arguments within its scope inside the function If the control flow of the Ch program is outside a function the symbol table contains global variables The index 303 Chapter A APIs for Embedding Ch lt embedch h gt number for the symbol table starts with 0 Example See Programs 2 4 and See Also Ch SymbolTotalNum Ch_SymbolIndexByName Ch_SymbolNameByIndex 304 Ch_SymbolAddrByIndex Ch_SymbolAddrByName Chapter A APIs for Embedding Ch lt embedch h gt Ch SymbolAddrByName Ch SymbolAddrByName Synopsis include lt ch h gt void Ch_SymbolAddrByName ChInterp_t interp const char name Purpose Get the address of a variable in the symbol table for variables and arguments of a function or in the symbol table for global variables in a Ch program Return Value The address of a variable in a Ch program If name is not in the list of the symbol table the function returns NULL If name is a tag name for class structure or union the function returns NULL For a variable of fun
316. next tmpl gt next tmpl gt next gt next free tmp2 gt u func funcname if tmp2 gt condexpr free tmp2 gt condexpr if tmp2 gt condvalue free tmp2 gt condvalue free tmp2 return 0 tmpl tmpl gt next return 0 remove a breakpoint from the list for a monitoring variable int chClearBreakPointVar debugInfo t dgptr char varname breakPoint t tmpl tmp2 tmpl dgptr gt bkpt if tmpl NULL return 0 else if strcmp tmpl gt u var varname varname dgptr gt bkpt tmpl gt next free tmpl gt u var varname if tmpl gt condexpr free tmpl gt condexpr if tmpl condvalue free tmpl gt condvalue free tmpl else while tmpl gt next if strcmp tmpl next u var varname varname tmp2 tmpl gt next tmpl gt next tmpl gt next gt next free tmp2 gt u var varname if tmp2 gt condexpr free tmp2 gt condexpr if tmp2 gt condvalue free tmp2 gt condvalue free tmp2 return 0 tmpl tmpl gt next return 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 188 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER int chClearBreakPoints debugInfo t dgptr breakPoint t bp head head dgptr gt bkpt while head bp head head head gt next if bp gt btype ACTION SETBREAK
317. ng Ch lt embedch h gt Ch Flush Synopsis include lt embedch h gt int Ch Flush ChInterp t interp ChFile t filedes Purpose Flush the contents of the buffer associated with a file descriptor Return Value Upon successful completion Ch Flush returns 0 Otherwise it returns EOF Parameters interp A Ch interpreter fildes The file descriptor for the stream to be flushed Description Ch Flush The Ch Flush function calls fflushQ to flush the stream associated with the file descriptor filedes re turned from function Ch Reopen See Also Ch Reopen Ch Close 256 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgArrayDim Ch FuncArgArrayDim Synopsis include lt ch h gt int Ch FuncArgArrayDim ChInterp t interp const char funcname int argnum Purpose Obtain the dimension for an argument of array type for a function of global variable in the Ch space Return Value If the argument of the function for a global variable in the Ch space is of array type This function returns the dimension of the array Otherwise it returns 0 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument Description This function returns the dimension of array type for a function of global variable in the Ch space The extent of each dimension of the array can be obtained b
318. nism for interface between Ch and C for such callback functions The procedure for calling C functions from the Ch space is still the same as one described in Ch SDK User s Guide The binary module with interface wrapper is loaded first by function dlopen Then the ad dress of a function is obtained by function dlsym Finally the function is invoked by function dlrunfun The dynamically loaded module is the application program itself in this case The header file sample h for both C and Ch programs is shown in in Program 5 4 The handler Chsample handle for the dynam ically loaded module is obtained by the programming statement Chsample handle dlopen NULL RTLD LAZY instead of Chsample handle dlopen libsample dl RTLD LAZY The first argument of function dlopen is NULL In HP UX the handler Chsample handleis NULL which is the default value in Ch when Chsample handle of pointer to void type is declared The code which is Ch specific is included in the header file sample h in Program using the predefined system macro CH The statement atexit dlclose sample makes the user defined function dlclose sample be called at the end of execution of program program ch to close the dynamically loaded library Program 5 2 calls two Ch functions func1 and func2 through function files in Programs and respectively Programs 5 5 and 5 6 each makes a call to its corresponding chdl function using dirunfun
319. ns int linefunceng the line number where the definition of the function ends const char source the file name if the source is a file otherwise it is a string It contains the first 70 characters beginning with string const char funcname function name if the block is a function otherwise NULL const char classname class name if it is a member function otherwise NULL int isconstructor true if it is a constructor of class otherwise false int isdestructor true if it is a destructor of class otherwise false ChBlock t Field event gives the event which triggers the callback function being called To obtain the event which caused the callback function be called can be obtained by the bit wise and operator amp For example the 212 Chapter A APIs for Embedding Ch lt embedch h gt Ch AddCallback events just after a Ch program calls and enters a new function and just before a Ch program leaves a function can be obtained inside a callback function as follows void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata callback at the beginning of a function call if calldata event amp CH MASKCALL printf event MASKCALL is true n if calldata gt event amp CH MASKRET printf event MASKRET is true n Field count gives the number of count instructions after Ch executes to trig
320. nt calldata contains the block information for the active function or program when the code executed is outside a function for the case of a line event The type ChBlock t is a structure containing the information for a block used by functions Ch AddCallback and Ch ChangeStack typedef struct ChBlock int event event for callback int count every count instructions for CH MASKCOUNT int level int level2 levell int level level0 int leveO current func int linecurrent the current line num where the program S executing he line number where the definition of i a int linefuncbegin t the function begins t IH t i he line number where the definition of the unction ends int linefuncend Ps const char source t is a string It contains the first 70 characters beginning with string const char funcname function name if the block is a function 129 he file name if the source is a file otherwise CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION otherwise NULL const char classname class name if it is a member function otherwise NULL int isconstructor true if it is a constructor of class otherwise false int isdestructor true if it is a destructor of class otherwise false
321. nt n struct tag s Ch DeclareFunc interp double func2 double x double y ChFuncdl t func2 chdl Ch ExprEval interp printf func2 2 3 Sf n func2 2 3 struct tag s Ch DeclareFunc interp tagptr t func3 tagptr t sp struct tag s Ch ExprEval interp printf s2 i d n s2 i SPECIAL CASE for function with variable number of arguments Ch DeclareFunc interp int func5 int n ChFuncdl t func5 chdl Ch ExprEval interp func5 3 10 12 34 Ch End interp return 0 Output x in funcl is 10 000000 x in func2 is 2 000000 y in func2 is 3 000000 func2 2 3 5 000000 sp gt i in func3 is 10 6 1 in func3 is 10 sp i 100 s i in func4 is 100 s2 i 1000 i in vfunc5 10 d in vfunc5 12 340000 Example 2 See Program See Also Ch DeclareTypedef Ch DeclareVar 240 Chapter A APIs for Embedding Ch lt embedch h gt Ch DeclareTypedef Ch DeclareTypedef Synopsis include lt embedch h gt int Ch DeclareTypedef ChITnterp t interp const char name Purpose Change a system variable to a typedefed data type in the Ch space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter name A system variable in the Ch space Description Function Ch DeclareVar can be used to declare system variables of valid data type in Ch including sim ple type pointer typer struct and
322. nt status char argvv userdefinedsymbol ch NULL Ch Initialize amp interp NULL status Ch RunScript interp argvv processUserDefined interp s processUserDefined interp sp processUserDefined interp sa x Ch End interp return 0 int processUserDefined ChInterp t interp const char name int i n 1 index totalnum char addr ChUserDefinedTag t udtag ChUserDefinedInfo t udinfo ChMemInfo t meminfo if Ch ArrayType interp name amp amp Ch DataType interp name CH STRUCITYPE Ch DataType interp name CH CLASSTYPE n Ch ArrayExtent interp name 0 assume it is 1 dim array addr Ch SymbolAddrByName interp name if Ch DataType interp name CH STRUCTPTRTYPE Ch DataType interp name CH CLASSPTRTYPE addr void addr printf The address of the variable p n addr udtag Ch UserDefinedTag interp name printf The user defined tag is p n udtag Ch UserDefinedInfo interp udtag amp udinfo printf udinfo dtype CH STRUCTTYPE d n udinfo dtype CH STRUCTTYP p printf The size of the struct class union is d n udinfo size printf The tag name of the struct class union is s n udinfo tagname printf The total number of members is d n udinfo totnum for i 0 i lt n itt for index 0 index lt udinfo totnum index t Ch UserDefinedMemInfoByIndex in
323. ntFunc dgptr name condexpr condtrue break case ACTION SETBREAKPOINTVAR ptr linetstrlen stopvar Sscanf ptr s s d name condexpr condtrue chSetBreakPointVar dgptr name condexpr condtrue break case ACTION CLEARBREAKPOINTLINE ptr line tstrlen clearline sscanf ptr s d name slinenum chClearBreakPointLine dgptr name linenum break case ACTION CLEARBREAKPOINTFUNC ptr linet strlen clearfunc chClearBreakPointFunc dgptr ptr break case ACTION_CLEARBREAKPOINTVAR ptr linet strlen clearvar chClearBreakPointVar dgptr ptr break case ACTION_CLEARBREAKPOINTS chClearBreakPoints dgptr break case ACTION_HELP chHelp break case ACTION ASSIGN ptr line strlen assign if chAssign dgptr ptr CH ERROR printf Error Ss is not valid in this scope n ptr break case ACTION CALL ptr line strlen call if chCall dgptr ptr CH ERROR printf Error Ss is not defined in this scope n ptr break case ACTION PRINT ptr line strlen print if chPrint dgptr ptr CH ERROR printf Error W sV is not defined in this scope n ptr break case ACTION EXPR if chExpr dgptr line CH ERROR printf Error W sV is not defined in this scope n line break case ACTION ABORT chAbort dgptr break default ACTION UNDEFINED ptr strchr line if ptr NULL ptr mu NOT printf
324. ntain preprecessing directives and comments delimited by To overcome these restrictions Func tions Ch AppendRunScriptFile and 13 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 4 INCREMENTAL EXECUTION BY APPENDING CODE TO AN EMBEDDED CH PROGRAM File Name append c Executing Ch program in C proram with embedded Ch Append code after embedded Ch program has executed finclude lt embedch h gt include lt stdio h gt include lt string h gt include lt stdlib h gt char codel fun 10 void fun2 int i printf W i in fun2 d n i X J fun2 20 char code2 X void fun3 int i fun 100 N printf N i in fun3 p Sd n i int main ChInterp t interp int status char argvv embedch2 ch opti opt2 NULL ChOptions t option int retval initialize embedded Ch option shelltype CH REGULARCH option chhome strdup embedch Embedded Ch Home Ch Initialize amp interp amp option run an Embedded Ch program indicated by argvv status Ch RunScript interp argvv if status CH ERROR printf Error execution of program embedch2 ch failedin status Ch AppendRunScript interp codel if status CH ERROR printf Error Ch AppendRunScript interp codel failed n Ch AppendParseScript will parse but not execute code status Ch AppendParseScript inte
325. nted from accesschvar ch ii 10 dd 1 100000 ii is int dd is double Ch DataSize interp dd 8 In C before assignments ii 10 dd 1 100000 In C after assignments ii 20 dd 2 200000 Figure 2 1 Output of executable compiling from accesschvar c int i 10 double d 1 1 int prototype extern int e struct tag ine Jy s int funct int localvariable 20 printf printed from symbol ch i d d fWMn i d return 0 int main Fune 3 return 0 Program 2 3 A Ch program for testing its symbol table symbol ch printf In C before assignments ii d dd f n pii pdd prints out values in these two addresses Afterwards assignment statements below pii 20 pdd 2 2 change values of ii and dd in the Ch program The output from executing the program built from Pro gram 2 TJis shown in Figure PI Symbols in header files including system header files are treated the same as in a user s program AI symbols in the symbol table for global variables in an embedded Ch program can be accessible by the hosting program after the Ch program is loaded by Ch RunScript Ch ParseScript or other functions Pro gram 3 has seven symbols i d prototype e s func and main The function prototype prototype and user defined functions func and main are included in the symbol table The symbol table does not contain generic functions printf system functions tag name
326. nts of Assumed Shape Arrays The methods described in this section can apply to Ch functions with arguments of both C arrays and Ch computational arrays of assumed shape Assume that the Ch function funct defined below takes two one dimensional assumed shape arrays as arguments int funct int a int b After the Ch program is loaded and executed function funct can be called in C space as follows int dim ext a ext b int retval int aal 3 1 2 3 int bb1 2 4 5 PR xk Mf dim 1 ext a 3 ext b 2 dim and extent of aal and bbl Ch CallFuncByName interp funct amp retval aal dim ext a bbl dim ext b PE wow ei The arguments aa1 and bb1 of function Ch_CallFuncByName are arrays in C space to be passed to the Ch function as arguments In most cases the number of the arguments following the third argument of API Ch CallFuncByName matchs exactly with the number of the arguments of the Ch function to be called This rule doesn t apply to the case of calling a Ch function with arguments of VLAs because VLAs in Ch argument lists can provide more information in addition to addresses whereas those in C argument list can t So the users need to provide extra arguments about dim and extents of array explicitly when a Ch function with arguments of VLAs is called from C space In the above function definition although the Ch function funct takes only two arguments the extra arguments dim ext
327. o Wa HOY 9 10 1l 127 double aa2 2 3 4 ll lE Gp 5 6 7 8 9 10 Lt 12 initialize embedded Ch Ch Initialize amp interp NULL run a ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program callchvlaNd ch failed n dim 2 extl 3 ext2 4 dimension and extents of aal Ch CallFuncByName interp sumNd amp sum ret aal dim extl ext2 printf sumNd has been called from C space and returned f n n sum ret dim 3 extl 2 ext2 3 ext3 4 dimension and extents of aa2 Ch CallFuncByName interp sumNd amp sum ret aa2 dim extl ext2 ext3 printf sumNd has been called from C space and returned f n sum ret Ch End interp Program 6 5 Example of calling Ch function with arguments of arrays of reference callchvlaNd c ext 0 3 4 sumNd has been called from C space and returned 78 000000 x a p ext 0 2 ext 1 3 ext 2 4 sumNd has been called from C space and returned 156 000000 Figure 6 2 Output from executing callchvlaNd exe 123 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt array h gt include lt stdio h gt array int funcl 2 3 array int a 2 3 a 10 return a array int func2 int n int m array int a n
328. obal variable or an expression in a Ch program Return Value Similar to the sizeof operator in C this function returns the size of of a local or global variable or an expression in a Ch program in the number of bytes Parameters interp A Ch interpreter name Name of a variable or expression whose data type is to be obtained Description The function Ch_DataSize gets the size of a variable or expression in the Ch space within the executed program scope If a passed argument is of array type the function will return the number of bytes for the array Example See Ch_DataType See Also Ch_DataType Ch ArrayDim Ch ArrayExtent Ch ArrayType Ch FuncType Ch FuncArgNum Ch IsFuncVarArg Ch UserDefinedTag Ch UserDefinedInfo 228 Chapter A APIs for Embedding Ch lt embedch h gt Ch DataType Ch DataType Synopsis include lt ch h gt ChType t Ch DataType ChInterp t interp const char name Purpose Get the data type of a local or global variable named name in a Ch program Return Value This function returns a macro for data type ChType t defined in header file ch h described in Ch SDK User s Guide If a passed argument is of array type the function will return the type of the element of the array For example CH INTTYPE and CH DOUBLETYPE are two macros defined for data type of int and double in Ch For a variable of C array or Ch computational array it gives the data type of its element Par
329. of arguments of function arrayfunc2 is 2 See Also Ch DataSize Ch ArrayDim Ch ArrayExtent Ch ArrayType Ch FuncArgNum Ch IsFuncVarArg Ch UserDefinedTag Ch UserDefinedInfo 235 Ch DataType Ch FuncType Chapter A APIs for Embedding Ch lt embedch h gt Ch DeclareFunc Ch DeclareFunc Synopsis include lt embedch h gt int Ch DeclareFunc ChInterp t interp const char funcprototype ChFuncdl t funcptr Purpose Declare and define a system function in the Ch space corresponding to a binary function in the C C space Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter funcprototype A function prototype for the defined function funcptr A pointer to a wrapper function to interface the binary function Description Function Ch DeclareFunc can be used to call a binary function in the C space from the Ch space in an Embedded Ch engine Unlike using Ch SDK no extra function files with file extension chf and dynamically loaded libraries with file extension dl are needed In addition to global variables a Ch environment contains system variables such as path and fpath The system variables are persistent When a new program is loaded these system variables remain in the system Similar to Ch DeclareVar functions defined by Ch DeclareFunc are system functions in the Ch space as if the function prototype funcptototype is pre appe
330. oft Visual Studio VC98 Bin C Program Files Microsoft Visual Studio Common MSDev98 Bin putenv stradd LIB C Program Files Microsoft Visual Studio VC98 Lib C Program Files Microsoft Visual Studio VC98 MFC Lib getenv LIB putenv stradd INCLUDE C Program Files Microsoft Visual Studio VC98 include C Program Files Microsoft Visual Studio VC98 MFC include getenv INCLUDE The code above defines the search paths for all the programs that will be run in the VC environment Ch will look through path to find the c1 exe command that is used to compile programs The LIB path will be searched for libraries Similarly the INCLUDE paths will be searched for the header files In Windows command CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 2 BUILDING EXECUTABLES IN WINDOWS nmake f makefile or make f makefile win will generate executable programs defined in the makefile To run a generated program simply type the command such as embedch1 exe at the Windows prompt 1 2 2 Building Executables in Windows Using Visual NET Assume Ch is installed in the directory C NCh To compile the code using Embedded Ch SDK in Visual NET follow the procedures below e Create a Win32 project or Win32 console project e Header files ch h and embedch h are located in the directory CHHOME extern include Some sample programs described in this document use header files in the current working dir
331. og o sampclass o sampclass chdl o prog o prog cpp ch dlcomp libclass dl cplusplus prog cpp sampclass o sampclass cpp ch dlcomp libclass dl cplusplus sampclass cpp sampclass chdl o sampclass chdl cpp ch dlcomp libclass dl cplusplus sampclass chdl cpp clean rm f o obj dl exe lib exp Program 5 33 Makefile to create prog exe for Unix Makefile 104 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE target prog exe prog exe prog obj sampclass obj sampclass chdl obj ch dllink prog exe cplusplus embedded prog obj sampclass obj sampclass chdl obj prog obj prog cpp ch dlcomp libclass dl cplusplus prog cpp sampclass obj sampclass cpp ch dlcomp libclass dl cplusplus sampclass cpp sampclass chdl obj sampclass chdl cpp ch dlcomp libclass dl cplusplus sampclass chdl cpp clean rm f o obj dl exe lib exp Program 5 34 Makefile to create prog exe for Windows Makefile Similar to C programs if applications programs in C such as prog exe are created using other au tomation process such as integrated development environment compile option DWINDOWS DLINUX DLINUXPPC DSOLARIS DHPUX DDARWIN DFREEBSD DONX should be added when pro grams such as sampclass chdl cpp with exported functions are compiled for Windows Linux So laris HP UX Mac OS X FreeBSD QNX respectively The binary objects should be linked with library embedc
332. ogram embedch2 ch calling the callback list in callback in Ch is 26930 list in callback chdl in C is 26930 list in printlist in C is 26930 list 0 stringl list 1 string2 after calling the callback Figure 5 3 Output of executable compiled from Program 93 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 3 CALLING BACK C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File prog3 c include lt stdio h gt include lt string h gt include lt embedch h gt include sample h EXPORTCH int printdata chdl void varg double data 4 10 0 20 0 30 0 40 0 int main int argc char argv ChInterp t interp int status int num retval char argvv embedch3 ch NULL Ch Initialize amp interp NULL Ch DeclareFunc interp int printdata int num double data ChFuncdl t printdata chdl status Ch RunScript interp argvv if status CH ERROR printf Error Ch RunScript failed n exit 1 num sizeof data sizeof double printf calling the printdata in Ch n status Ch_CallFuncByName interp printdata amp retval num data printf retval d n retval printf after calling the printdata in Ch n if status CH ERROR printf Error Ch CallFuncByName failed n exit 1 Ch End interp int printdata int num double data Ent u for i 0 i num i printf data d Sf n i
333. ointer t Ch GetGlobalUserData ChInterp t interp Purpose Get the global user data for an instance of the Embedded Ch engine Return Value This function returns the global user data of generic data type if it was set by function Ch SetGlobalUserData previously Otherwise it returns NULL Parameters interp A Ch interpreter Description When the Embedded Ch is embedded in a multi engine environment this function allows the user to handle different instances The global user data for an Embedded Ch engine set by function Ch SetGlobalUserData 1s obained by this function Example See Ch SetGlobalUserData See Also Ch SetGlobalUserData 276 Chapter A APIs for Embedding Ch lt embedch h gt Ch GlobalSymbolAddrByIndex Ch GlobalSymbolA ddrByIndex Synopsis include lt ch h gt void Ch_GlobalSymbolAddrByIndex ChInterp_t interp int num Purpose Get the address of a global variable in a Ch program Return Value The address of a global variable in a Ch program If the index number is out of the valid range for its symbol table the function returns NULL If the symbol is a tag name for class structure or union the function returns NULL For a variable of function prototype without function definition or an extern variable without definition the function returns NULL For a variable of C array Ch computational array or pointer to Ch computational array the returned value is the address of the first element F
334. olIndex Ch Close Ch DataSize Ch_DataType Ch_DeclareFunc Ch_DeclareTypedef Ch_DeclareVar Ch_DeleteExprValue CH DOUBLETYPE see macros see macros Ch_End CH_ERROR Ch_ExecScript 9 Ch ExecScriptM Ch ExprCalc Ch ExprEval Ch ExprParse Ch ExprValue Ch_Flush 22 Ch_FuncArgArrayDim Ch_FuncArgArrayDim 201 Ch_FuncArgArrayExtent Ch_FuncArgArrayExtent Ch_FuncArgArrayNum 259 Ch_FuncArgArrayType Ch_FuncArgArrayType Chapter B APIs for Embedding Ch lt embedch h gt Ch FuncArgDataType Ch FuncArgDataType Ch FuncArgFuncArgNum Ch FuncArgFuncArgNum Ch FuncArgIsFunc Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch_FuncArgIsFunc VarArg 202 271 Ch_FuncArgNum Ch_FuncArgUserDefinedName Ch_FuncArgUserDefinedName Ch_FuncArgUserDefinedSize Ch_FuncArgUserDefinedSize CH FUNCCONSTYPE CH FUNCDESTTYPE CH FUNCMEMBERTYPE CH FUNCPROTOTYPE CH FUNCPTRTYPE CH FUNCTYPE Ch_FuncType Ch_GetGlobalUserData 202 Ch_GlobalSymbolAddrByIndex Ch GlobalSymbolAddrByNameQ 29 202 204 Ch GlobalSymbolIndexByName Ch GlobalSymbolNameBylIndex Ch GlobalSymbolTotalNum CH GLOBALVARTYPE Ch Homel Ch InitGlobalVar Ch Initialize CH INTTYPE see macros see macros CH INVALIDLEVEL Ch IsFunc Ch IsFuncVarArs Ch IsVariable CH LOCALVARTYPE CH MASKABORT CH MASKBLOCK CH MASKCALL CH MASK
335. on Example In this example there are two global variables in program expreval ch ii is an integer and dd is a value of double type We can evaluate the expression dd dd ii from C space by function Ch ExprEval and get the new value of dd by function Ch SymbolAddrByName We can call the Ch function fun through evaluating expression fun in expreval c chexpreval c A C program to evaluate expressions in Ch address space KKK kk kk kk kk kk kk kk kk kk kk kk kk kk kk KCKCKCKCKCKCkCKCKCk kk kk k Ck k k k k k Ck k Ck ck k kk kk ke kk Execute expression and function in Ch address space OXCKCKCKCKCKCKCKCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCKCKCKCKCkCKCKCKCKCKCkCKCkCkCk Ck k ck k ck k ck ck k ck ck ck ck ck kk k ok ke ke ke e Ke x include lt stdio h gt include lt ch h gt EXPORTCH void chexpreval chdl void varg ChInterp t interp ChVaList t ap Tnt Duo double pdd Ch VaStart interp ap varg pii Ch SymbolAddrByName interp ii pdd Ch SymbolAddrByName interp dd printf ii Sd n pii 251 Chapter A APIs for Embedding Ch lt embedch h gt printf dd f n pdd if Ch_ExprParse interp fun Ch ExprEval interp fun else printf Error fun is invalid Ch expression n Ch ExprEval interp dd dd ii printf XnAfter dd dd ii n printf ii d n pii printf dd f n pdd Ch ExprEval interp fun
336. on and its value in the list of watched expressions and variables In a typical GUI based debugger the function chProcessWatchExprs needs to be modified to display expressions and their corresponding values in a separate window Function chSetBreakPointLine sets a new breakpoint specified by a file name and line number The program breaks execution when it reaches this location Function chSetBreakPointFunc sets a new breakpoint for a function The program breaks execution when it reaches the first executable line of the function Function chSetBreakPointVar sets a new breakpoint for a controlling variable The variable is evaluated while the program is running The program breaks execution when the value of the variable changes When each of these functions is called a breakpoint is appended to the linked list of breakpoints The optional conditional expression and triggering method for each breakpoint are passed as the last two arguments of these functions For example the syntaxes for setting a breakpoint in a file and line number are as follows debug stopat filename f debug stopat filename condexpr debug stopat filename condexpr condtrue When a breakpoint location is reached the optional expression condexpr is evaluated If the argument condtrue is true or missing the breakpoint will be hit if the value for the expression is true otherwise the breakpoint will be hit if the value for the expression has ch
337. on with argument of arrays of reference from C space Assume that the Ch function funct defined below takes an array of reference with one dimension as argument int funct array int a amp After the Ch program is loaded and executed function funct can be called in C space as follows int dim ext int retval int aal 3 1 2 3 E zem R dim 1 dimension of aal ext 3 extents of aal Ch CallFuncByName interp funct amp retval aal dim ext GR osa E 118 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv callchvla2d ch NULL int dim extl ext2 double sum_ret double aal 2 3 1 2 3 4 5 6 double aa2 3 4 1 2 3 4 5 6 7 8 910 Liye 12 initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program callchvla2d ch failed n dim 2 dimension of aal and aa2 xtl 2 ext2 3 extents of aal Ch CallFuncByName interp sum2d amp sum ret aal dim extl ext2 printf sum2d has been called from C space and returned f n n sum ret xtl 3 ext2 4 extents of aa2 Ch Ca
338. ond time by API Ch_CallFuncByNameVar it uses values in the C space It is equivalent to calling function func by func n m f p a b d S Sp 56 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 5 CALLING CH FUNCTIONS BY CH CALLFUNCBYNAMEVARO File Name callfuncbynamevar c include lt stdio h gt include lt embedch h gt struct tag int i double f s 10 20 sp amp s int main ChInterp t interp int n 2 m 3 p sn double f 10 0 int a 2 3 1 2 3 4 5 6 int b 2 3 1 2 3 4 5 6 int c 2 3 1 2 3 4 5 6 int d 2 3 1 2 3 4 5 6 int status retval dim 2 char argvv callfuncbynamevar ch NULL ChVaList_t arglist Ch Initialize amp interp NULL status Ch RunScript interp argvv if status CH ERROR printf Error execution of program callfuncbynamevar ch failed n use variables and expressions in Ch space arglist Ch VarArgsCreate interp h VarArgsAddArgExpr interp amp arglist 2 h VarArgsAddArgExpr terp amp arglist m c h VarArgsAddArgExpr terp amp arglist f c h VarArgsAddArgExpr terp amp arglist p c h VarArgsAddArgExpr terp amp arglist a c h VarArgsAddArgExpr terp amp arglist b c h VarArgsAddArgExpr terp amp arglist o c h VarArgsAddArgExpr terp amp arglist d c h VarArgsAddArgExpr terp amp arglist s ch h VarArgsAddArgExpr interp amp ar
339. or a variable of pointer to C array the returned value is the address of the pointer to C array i e the address of the first element For example for variables of a and p declared in Ch int pa 3 p al2 3 the returned values for both pa and p are the same of pointer to pointer to int The return value for a is the address of the first element a 0 0 The data type for variable a returned from Ch_DataType is CH_INTTYPE whereas the data type for variables pa and p returned from Ch_DataType is the same as CH_INTPTRTYPE Note that in C the address of function is the same as the function For example the output from the two printing statements below are the same int Pune 41 f printf address of func p n func printf address of func p n amp func Parameters interp A Ch interpreter num The index number of global variable in the symbol table in a Ch program Description Based on its index number of a global variable in the symbol table the function Ch GlobalSymbolAddrByIndex obtains the address of the global variable inside a dynamically loaded library or embedded Ch program The index number for the symbol table starts with 0 Example 1 See Program 2 4 Example 2 In this example function func in Ch will be called Just before the function func returns the callback function callback in the C space will be executed APIs starting with Ch GlobalSymbol 277 Chapte
340. owing two statements 121 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt array h gt double sumNd array double amp a int i totnum 1 double retval 0 int dim int shape shape a array int ext dim ext shape a for i 0 i lt dim i printf ext Si totnum ext i array double aa totnum aa array int totnum a for i 0 i lt totnum i retval aa i return retval sd n i ext i function to be called from C space Program 6 4 Example of calling Ch function with arguments of arrays of reference callchvlaNd ch Ch VarArgsAddArg interp Ch VarArgsAddArg interp amp arglist amp arglist CH INTTYPE CH INTTYPE n m The values of the returned array are passed back and saved in the memory for arrays a and b in the C space in the third argument of functions Ch CallFuncByName and Ch CallFuncByNameVar The output of Program 6 7 is shown in Figure 6 3 122 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 1 CALLING CH FUNCTIONS WITH VLAS AND COMPUTATIONAL ARRAYS FROM C SPACE include lt stdio h gt include lt string h gt include lt embedch h gt int main ChInterp t interp int status char argvv callchvlaNd ch NULL int dim extl ext2 ext3 double sum_ret double aal 3 4 1 2 3 4 Dat uom
341. p ch funcl ap ch CH DOUBL from C space Mn Ch VarArgsCreate interp CH INTTYPE CH FLOATTYPE amp retval failed n ETY amp retval from C space n Ch VarArgsCreate interp kap ch kap ch func2 kap ch t nc2 ap ch CH DOUBL CH INTTYPE CH FLOATTYPE amp retval ap c 3 f i ap ch ETY PE d amp retval from C space n Ch_VarArgsCreate interp kap ch kap ch Mure 3s kap ch runc ap ch CH DOUBL rune CH INTTYPE CH FLOATTYPE amp retval i ap ch j f ap_ch EIY PE amp retval amp retv d h ap c al ap ch Program 3 11 Calling Ch functions taking variable number of arguments callchvna c 63 CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS calling funcl from C space vacount 2 val i 10 val f 20 000000 vacount 3 val i 10 val f 20 000000 val d 40 000000 calling func2 from C space vacount 2 val i 10 val f 20 000000 vacount 3 val i 10 val f 20 000000 val d 40 000000 calling func3 from C space vacount 2 val i 10 val f 20 000000 vacount 3 val i 10 val f 20 000000 val d 40 000000 Figure 3 5 Output of executable compiled from Program B 11 Besides using APIs Ch
342. pa pa int Ch SymbolAddrByName interp a to int pa pa int Ch SymbolAddrByName interp a Functions Ch UserDefinedName and Ch UserDefinedSize are deprecated Use Ch UserDefinedInfo Change 318 Chapter B APIs for Embedding Ch lt embedch h gt Ch VarType imt Size char name size Ch UserDefinedSize in name Ch UserDefinedName int rp sS Ep ste CE ct to int size char name ChUserDefinedTag t udtag ChUserDefinedInfo t udinfo udtag Ch UserDefinedTag interp s Ch UserDefinedInfo interp udtag amp udinfo size udinfo size name udinfo tagname 5 Function Ch SymbolIndex is deprecated Use Ch SymbolIndexByName Change index Ch SymbolIndex interp name to index Ch SymbolIndexByName interp name 6 Function Ch IsFunc is deprecated Use Ch FuncType Change if Ch IsFunc interp name to if Ch FuncType interp name 7 Function Ch_IsVariable is deprecated Use Ch_VarType Change if Ch_IsVariable interp name to if Ch_VarType interp name 319 Chapter B APIs for Embedding Ch lt embedch h gt Ch VarType 8 The deprecated members chrc chrcname chmtdir of struct ChOptoin t have been removed See section L 7 on how to distribute applications with Embedded Ch 9 The location for the distribution of dynamically loaded lib chmt dl in Windows has
343. project files for the example de scribed in section are located in CHHOME toolkit demos embedch chapters chapter5 embedclassmain Section B 6 il lustrates invoking C classes from Ch space using a dynamically loaded library libsample dl file 5 1 Calling C Functions in the Main Program from Ch Space Program S5 T invokes a Ch script program ch shown in Program 5 2 The output of executable compiled from Program 5 1 is shown in Figure Program 5 2 calls two C functions func1 and func2 78 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE File name prog c include lt stdio h gt include lt embedch h gt include sample h int main ChInterp t interp char argvv program ch NULL int status Call funcl in func c double d d funcl 3 0 printf funcl 3 0 in prog c f n d initialize embedded Ch Ch Initialize amp interp NULL run a Ch program status Ch RunScript interp argvv if status printf Error execution of program ch failed n Ch End interp Program 5 1 C program invoking embedded Ch program prog c File name program ch finclude lt stdio h gt include sample h int main double dl d2 dl sin 3 0 d2 hypot 3 4 printf sin 3 f n dl printf hypot 3 4 f n d2 di funclis U d2 func2 3 4 printf funcl
344. r platforms Normally registered functions in atexit for Embedded Ch programs are executed in Ch End When a Ch script is executed by Ch ExecScriptM or Ch RunScriptMO functions registered in atexit will be executed when the script is executed Therefore a single instance of Ch interpreter initialized by Ch Initialize can be used to process multiple Ch scripts using Ch ExecScriptM or Ch RunScriptM Example Refer to Programs LI and See Also Ch Home Ch SymbolAddrByName Ch DataType Ch ExprEval Ch_ExecScript Ch ExecScriptM Ch RunScript Ch RunScriptM Ch End 287 Chapter A APIs for Embedding Ch lt embedch h gt Ch IsFuncVarArg Ch IsFuncVarArg Synopsis include lt ch h gt int Ch_IsFuncVarArg ChInterp_t interp const char name Purpose Determine if a variable in the Ch space is a function or pointer to function Return Value If the argument is a function type with a variable number of arguments this function returns 1 Otherwise it returns 0 Parameters interp A Ch interpreter name Name of the variable in the Ch space Description The function Ch_IsFuncVarArg determines if the argument is a function pointer to function or member function of a class with a variable number of argument The function Ch_SymbolAddrByName can be used to test if a function has a definition or not For a variable of function prototype without function definition the function Ch_SymbolA
345. r A APIs for Embedding Ch lt embedch h gt Ch GlobalSymbolAddrByIndex are used to access variables in the symbol table for global variables globalsymbol c A C program to run globalsymbol ch KK kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk kk k kok k k k k Ck k kc k k ck k ck k ck k ck ck ck ok OK File Name symbol c CKCKCKCKCKCKCKCKCKCkCKCkCKCKCKCkCKCKCKCKCK kCKCKCKCKCKCKCKCKCKCKCKCKCKCkCKCk Ck k Ck k ck k k k ck kok ck kk k k ok ke ke ex include lt stdio h gt include lt embedch h gt void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int main ChInterp t interp char argvv globalsymbol ch NULL int mask count 0 ChPointer_t clientdata NULL Ch Initialize amp interp NULL mask CH MASKRET Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 void callback ChInterp t interp int index char name void addr if strcmp calldata funcname totalnum clevel hlevel p func Ch StackLevel interp amp clevel amp hlevel printf the current stack level is d n printf the highest stack level is d n totalnum Ch GlobalSymbolTotalNum interp printf The total number of symbols is d n for index 0 index lt totalnum index t name Ch GlobalSymbolNameByIndex interp addr Ch GlobalSymbolAddrByIndex interp printf symbol index d
346. r file shown in Program The global variable i1 in Ch is declared with the same data type as the corresponding global variable i1 in C It is used to keep the 70 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 2 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE original value of the C variable Since the value of i2 in C space will be changed during the execution of the program not only its value but also its address is needed in Ch space A temporary variable i2 is declared as pointer to int to keep the address of C space variable i2 in Program so that we can get the C space value of i2 from Ch space using macro i2 defined as follows define i2 i2 Then the chdl function expvar chdl defined in Program 4 4Jis called by statements void fptr expvar h dlsym ChExpvar handle expvar chdl dlrunfun fptr expvar h NULL NULL amp il amp i2 where addresses of variables i1 and i2 in Ch space are passed to C space as arguments of dlrunfun In the chdl function expvar chdl shown in Program 4 4 statements iil Ch VaArg interp ap int iil il pass the value to the Ch space assign the value of 11 to the address of variable i1 in Ch space Therefore the variable i1 in Ch has the same value as the one in the C space But if the variable 11 in C space is changed afterward the one in Ch will keep the original value For variable i2 statements ii2 Ch VaArg interp ap int
347. rCalc interp fun CH_INTTYPE else NULL printf Error fun is invalid Ch expression n Ch ExprCalc interp dd dd ii CH DOUBLETYPE amp result S amp result NULL printf NnAfter dd dd iin printf ii d n pii printf dd f n pdd printf result f n result Ch ExprCalc interp ii 2 CH DOUBLETYPE printf ii 2 f n result Ch ExprCalc interp fun CH VOIDTYPE Ch ExprCalc interp hypot 3 4 0 CH DOUBLETYPE amp result printf hypot 3 4 f n result Ch VaEnd interp ap return chexprcalc ch A Ch function file with global variables and functions BORK RK KR KR AXA XXX AX XXX RK OK Test the function Ch_ExprCalc in C space FR RK RR OR KA OR ke x include lt dlfcn h gt void chexprcalc void dlhandle fptr dlhandle dlopen libch dl RTLD LAZY if dlhandle NULL printf Error s dlopen s n return func dlerror fptr dlsym dlhandle chexprcalc chdl if fptr NULL printf Error s dlsym sin return dlrunfun fptr NULL chexprcalc func dlerror 249 Ch ExprCalc Chapter A APls for Embedding Ch lt embedch h gt Ch ExprCalc if dlclose dlhandle 0 printf Error s dlclose s n func dlerror return return
348. rayNum interp f4 4 printf number of elements of arg 2 of f5 is d n Ch FuncArgArrayNum interp f5 1 printf number of elements of arg 3 of f5 is d n Ch FuncArgArrayNum interp f5 2 printf number of elements of arg 4 of f5 is d n Ch FuncArgArrayNum interp f5 3 printf number of elements of arg 5 of f5 is d n Ch FuncArgArrayNum interp f5 4 printf number of elements of arg 6 of f5 is d n Ch FuncArgArrayNum interp f5 5 printf number of elements of arg 7 of f6 is d n Ch FuncArgArrayNum interp f6 6 printf number of elements of arg 1 of f8 is d n Ch FuncArgArrayNum interp f8 0 printf number of elements of arg 2 of f8 is d n Ch FuncArgArrayNum interp f8 1 printf number of elements of arg 3 of f8 is d n Ch FuncArgArrayNum interp f8 2 printf dim of arg 1 of f1 is dWMn Ch FuncArgArrayDim interp f1 0 printf dim of arg 4 of f1 is dWMn Ch FuncArgArrayDim interp f1 3 printf dim of arg 1 of f2 is dWMn Ch FuncArgArrayDim interp f2 0 printf dim of arg 2 of f4 is dWMn Ch FuncArgArrayDim interp f4 1 printf dim of arg 3 of f4 is dWMn Ch FuncArgArrayDim interp f4 2 printf dim of arg 4 of f4 is dWMn Ch FuncArgArrayDim interp f4 3 263 Chapter A APls for Embedding Ch lt embedch h gt Ch FuncArgDataType printf dim of arg 5 of f4 is dWMn Ch FuncArgArrayDim interp f4 4 printf dim of
349. re used inside a text Example variable var is declared in the program Directories are presented in typewriter like typeface when they are used inside a text Example Ch is installed in the directory usr 10ocal chin Unix and C Ch in Windows Environment Variables are the system level variables They are presented in boldface Example Environment variable PATH contains the directory usr ch Other Relevant Documentations The core Ch documentation set consists of the following titles These documentation come with the CD and are installed in CHHOME docs where CHHOME is the Ch home directory The Ch Language Environment Installation and System Administration Guide version 6 1 SoftIn tegration Inc 2008 This document covers system installation and configuration as well as setup of Ch for Web servers The Ch Language Environment User s Guide version 6 1 SoftIntegration Inc 2008 This document presents language features of Ch for various applications The Ch Language Environment Reference Guide version 6 1 SoftIntegration Inc 2008 This document gives detailed references of functions classes and commands along with sample source code The Ch Language Environment SDK User s Guide version 6 1 SoftIntegration Inc 2008 This document presents Software Development Kit for interfacing with C C functions in static or dynamical libraries The Ch Language Environment CGI Toolkit User s Guide
350. read function called before n if Ch Initialize amp interp NULL CH ERROR printf Error Ch Initialize failed in thread function in 206 Chapter A APls for Embedding Ch lt embedch h gt Ch Abort printf thread_function called2 end n if Ch AppendRunScript interp code CH ERROR printf Error Ch AppendRunScript failed in thread function in printf thread_function called3 end n if Ch_CallFuncByName interp func amp retval CH_ERROR printf Error Ch CallFuncByName failed in thread function in printf thread_function called4 end n Ch End interp return NULL void thread function2 void arg sleep 1 Ch Abort interp return NULL int main pthread t id 2 jb thread functionl NULL L thread function2 NULL pthread create amp id 0 pthread create amp id 1 pthread join id 0 NUL pthread join id 1 NUL return 0 aad Example 2 in Unix Abort the execution of a Ch script by Ch_Abort from a different thread The interpreter handle interp is a local variable You may need to add the lib for pthread by the option lpthread chabort_thread2 c A C program aborting a Ch script from a different thread include lt pthread h gt include lt stdio h gt include lt stdlib h gt include lt embedch h gt char code int func while 1 printf he
351. ree threads are created using POSIX function pthread create The first argument of function pthread create is the thread id the second one the attributes of the thread the third one the thread function and the last one the argument to the thread function In the thread function thread function Ch Initialize initializes an instance of Ch interpreter The Ch code in the memory is loaded using Ch AppendRunScript Function func in the Ch space is called by Ch CallFuncByName with the value of its argument passed from the fourth argument of function pthread create Each Ch interpreter in different threads has to be terminated by Ch End when it is no longer needed The process waits for all three threads to complete using the function pthread join Because these threads are executed simultaneously without a particular order a possible output from the application is as follows 1 hello 2 hello 32 Tello Program l 11 is an example of multi thread application with Embedded Ch in Windows The logic of Program is the same as that of Program LIO Instead of pthread create Windows API Cre ateThread is used to create three threads These three threads are joined in the process using WaitForSingleObject instead of pthread join Although the prototype of the thread function thread_function in Program LI is different from that in in Program the contents of the function as to how to embed Ch are the same Note tha
352. ring type string t The function returns a string with a value of the combination of the string from the input argument and string ABCD After program callstring ch is loaded functions func in the Ch space is called in the C space by function calls of Ch CallFuncByName interp func amp retvalp strl and Ch CallFuncByNameVar interp func amp retvalp arglist respectively Function func has one argument of string type The variable st r1 of pointer to char is passed to function func in the first call using Ch CallFuncByName from C The variable st r2 in C is passed to Ch function function func through an argument list in the last argument for the API Ch CallFuncByNameVar The argument list is built with a value of type of pointer to char in the C space using the API Ch VarArgsAddArg in the following statement Ch VarArgsAddArg interp amp arglist CH CHARPTRTYPE str2 The values of the returned string from the Ch function are assigned to the variable ret valp of pointer to char in C and printed out The allocated memory are freed afterwards The output of Program 6 7 is shown in Figure 6 4 126 CHAPTER 6 CALLING SPECIAL CH FUNCTIONS FROM C SPACE 6 2 CALLING CH FUNCTIONS WITH BUILT IN STRING TYPE STRING T include lt stdio h gt include lt embedch h gt int main ChInterp t interp char argvv callstring ch NULL int status char retvalp strl firstString str2 20 seco
353. rmation for each member of a variable of the user defined type can be obtained by functions Ch UserDefinedMemInfoByName and Ch UserDefinedMemInfoByIndex based on the member name and index of the member in the symbol table for the user defined type respectively As an example Program D 8 contains a user defined structure tag with four members i d p and a Three variables s sp and sa of structure pointer to structure and array of structure are declared For comparison a global variable i int type using the same name of a member of structure is also declared in the program Program first executes the script in Program 2 8 by function Ch RunScript It then obtain the information for structure t ag and its members by function processUserDefined The dimension of array for variable sa is obtained by Ch ArrayExtent Like variables of other type the address for a variable of structure t ag can be obtained by function Ch SymbolAddrByName The tag for structure is assigned to the variable udt ag The information for the variable s is kept in the variable udinfo whereas the variable memi nfo contains the information for a member of variable s Function printMemInfo prints out the information for a member Using the offset for each member of structure t ag the address for each member of a variable of structure tag can be obtained by the expression addr meminfo offset The output from executing Program 2 9 is shown in Fig
354. rmation including the directory of Embedded Ch used in the API Ch Initialize Members callback clientdata calldata and count contain the callback information re lated to Ch AddCallback The member level is used to keep track the function stack level for com mands up and down The remaining members runmode bkpt wexpr previousfuncname and stepovernum are used to handle breakpoints of different commands to execute a script The member runmode indicates which command invokes the current statement in the callback function The mem ber bkpt contains the head of the linked list of breakpoints The member wexpr contains the head of the linked list of watched expressions The member previousfuncname with the function name in the previous callback is used to implement a breakpoint of function type If the function name in the current call back is different from that in the previous callback the function breakpoint will be triggered The member stepovernumis used to implement the command next to step over a function The remaining part of the header file debug h contains function prototypes for external functions used in the sample debugger A external function name starts with the prefix ch such as chInitDebug Static functions in the file scope are also listed as comments in this header file They are separated into four filles Functions in file stack c in Program 7 9 are for printing symbols and their values in a symbol list Functions in callback
355. rminate the shell Ch End interp Program 1 1 A C program with embedded Ch embedch1 c gt embedchl exe hello from embedchl ch The Ch program embedch1 ch has been executed to generate the output Let s take a close look at Program I T to understand the procedure of embedding Ch in C C programs First the header file embedch h in which APIs are specially defined for embedded Ch must be included Next APIs are used to involve Embedded Ch Variable argvv is an array of pointers to char which is used to keep the file name of the Ch program to be executed as well as arguments passed to the Ch program The initialization char argvv embedchl ch NULL indicates that Ch program embedch1 ch will be called without argument The last element of argvv should be a NULL pointer Figure LT illustrates how a Ch program is executed by these three core APIs of embedded Ch The function call Ch Initialize amp interp NULL initializes a Ch interpreter with default options The startup file CHHOME config chrc will be executed in this initialization function The first argument is a pointer to ChInterp t a Ch interpreter The data type ChInterp t is defined in header file ch h All macros and functions defined in header file ch h are available in Embedded Ch The variable int erp for an instance of Ch interpreter is used in all subsequent calls The data type of the first argument of all Ch APIs is ChInterp_t except for fun
356. rocessActionMenu processes the user input Using a while loop the user can input commands multiple times For example the user can set multiple breakpoints or print out values of different variables during the debugging When the user types a command such as step to execute the script the while loop will terminate In the while loop it first calls the local static function getACtionMenu to obtain the user input The user input is then compared with the command string Some commands such as step have no argument whereas others such as load have an argument The user input is passed back to the calling function by the argument line of function getACtionMenu The getACtionMenu re turns an enumerate number corresponding to a command For each command a corresponding function in Program 7 16 is called inside the while loop of the function chProcessActionMenu To debug a program the user needs to specify a program first For example to load program prog1 ch the user may enter the command as follows debug gt load progl ch Function chLoadFile in Program 16 corresponds to the enumerate number ACTION LOAD in Pro gram 7 15 for the command load in Figure 7 10 The program name is copied to the member progname of the debugger Function chStart starts debugging a program The command optional arguments for the command start and run are processed by the local static function getArgvFromStr For example the command debug gt lo
357. rogram will stop at the first executable line of the function when the breakpoint is hit The field called is set to 1 when the first executable line of the function is being executed It is reset to 0 when the function returns so that the program will stop at the function again next time when it is called A breakpoint of controlling variable type needs both variable name varname and its previous value saved in a memory pointed by an opaque pointer varvalue When the controlling variable changes the breakpoint will be hit The members condexpr condvalue and condt rue are used to hold a conditional expression for a breakpoint The value for the conditional expression condexpr of different data type is saved in the memory pointed by an opaque pointer condvalue If condt rue is true that the conditional value is true triggers the breakpoint Oth erwise the change of the conditional value triggers the breakpoint The structure debigInfo_t contains 167 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER all information relevant to the debug The member progname of the structure debigInfo t contains the program loaded by the command load The member argvv with the program name and optional command line arguments up to the maximum number specified by the macro MAX NUM ARGS is passed to the API Ch ParseScript or Ch RunScript The member interp saves the interpreter instance The member option contains the info
358. rp code2 if status CH ERROR printf Error Ch AppendParseScript interp code2 failed n else status Ch ExprParse interp fun3 30 if status CH ERROR printf Error Ch ExprParse interp fun3 30 failed n else Ch_ExprEval interp fun3 30 release memory and terminate the shell free option chhome Ch End interp Program 1 7 Append code after embedded Ch program has executed 14 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS Ch AppendParseScriptFile can be used to append the code located in a file to an embedded Ch pro gram More information about using functions Ch AppendRunScript and Ch AppendRunScriptFile can be found in section 1 5 Embedding Ch in Multi Thread Applications In some applications multiple Ch programs need to be executed in a single Ch interpreter embedded in an application Function Ch RunScript or Ch ExecScript cannot be invoked multiple times through a single interpreter To execute multiple embedded Ch programs sequentially inside an application pro gram with a single instance of Ch interpreter functions Ch RunScriptM or Ch_ParseScript and Ch ExecScriptM shall be used For example only one instance of Ch interpreter is created by func tion Ch Initialize in Program After the start up file CHHOME config chre is executed through Ch Initialize embedded Ch programs embedch1 ch and e
359. rror s dlsym s n class_func__ dlerror return 1 dlrunfun fptr amp retval memfunl this i return retval Program 5 36 Ch member function file sampclass chf 107 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 5 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE m il in Classl Classl 1 Begin output from prog exe m il in Classl memfunl 11 End output from prog exe m il in Classl Classl lin m il in Classl Classl 1 m il in Classl Classl 1 m il in Classl Classl 1 m il in Classl memfunl 101 m il in Classl memfunl 201 m il in Classl memfunl 301 m il in Classl memfunl 301 m il in Class1 Class1 201 m il in Classl Classl 301 m il in Classl Classl1 301 m il in Class1 Class1 101 m il in Class1 Class1 11 Figure 5 4 Output of executable compiled from Program Program 5 32 runs a Ch script in Program with classes declared in the script But the actual definition of the class is defined inside C although there is a definition of the same class with constructor destructor and member functions only in Ch In some applications it may be desirable to invoke a class both defined and declared inside C Program 5 37 and Program 5 38 illustrate such an application scenario Program 5 38 contains a function func which has two arguments one is a pointer to class and the other is an integer Header file s
360. s include lt embedch h gt int Ch SetGlobalUserData ChInterp t interp ChPointer t userdata Purpose Set the global user data for an instance of the Embedded Ch engine Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter user the user data in a generic data type for an instance of the Embedded Ch Description When the Embedded Ch is embedded in a multi engine environment this function allows the user to handle different instances The global user data can be shared and accessed by Embedded Ch API functions within the same instance of the Ch engine using function Ch GetGlobalUserData Using a structure different data can be accessed by different APIs Example 1 The program has two instances of Embedded Ch engine Each has its own global user data s1 and s2 These data are passed to the function func in the C space File Name userdata c include lt stdio h gt include lt embedch h gt struct tag int i double d int func struct tag sp double x printf sp gt i d sp gt d printf x in func Sf n x return 0 f n sp gt i sp gt d EXPORTCH int func_chdl void varg ChInterp t interp ChVaList t ap ChPointer t userdata struct tag sp double x int retval Ch VaStart interp ap varg get the user data set by Ch SetGlobalUserData userdata Ch GetGlobalUserData interp 293
361. s s Mn calldata gt classname if calldata gt isconstructor printf Member function s s is a constructor Mn calldata gt classname calldata gt funcname if calldata gt isdestructor printf Member function s s is a destructor n calldata classname calldata gt funcname else if calldata gt funcname printf Function s starts at line d Wn calldata gt funcname calldata gt linefuncbegin printf Function s ends at line d Wn calldata gt funcname calldata gt linefuncend printf The function name is s n calldata gt funcname if calldata gt source if strncmp calldata gt source string strlen string printf The source code of the string is s n calldata gt sourcetstrlen string else printf The current file name is s n calldata gt source else printf No source information n return 0 void callback ChInterp t interp ChBlock t calldata ChPointer t clientdata int p printCalldata calldata p int Ch SymbolAddrByName interp i if p NULL printf i is not available n else printf i Sd n p print out client data and callback info once if calldata event amp CH MASKCALL ss strcmp calldata funcname main struct tag sp sp struct tag clientdata printf sp gt i d sp gt d f n sp gt i sp gt d printf Nn Pro
362. s at line 47 Function main ends at line 52 The function name is main The current file name is prog4 ch Function main at stack level 1 has 3 symbols func main i 10 x 0 The stack level is 2 The current line number is 0 The current file name is prog4 ch Program prog4 ch at stack level 2 has 81 symbols diclose 0x5a340 _Chstdio_handle OXff1f1470 dlclose libstdio 0X5ad40 x 40 ptri 0x5da50 g 100 0000 a 1 0000 2 0000 3 0000 4 0000 5 0000 b 1 0000 2 0000 3 0000 4 0000 5 0000 6 0000 ptrd 0X56d88 n 2 m 3 vi 1 0000 2 0000 v2 1 0000 0 0000 0 0000 0 0000 0 0000 6 0000 z 3 0000 14 0000 strl stringl str2 string2 str3 string3 str4 string41 string42 NULL s i 10 d 20 0000 sp OX5bcf0 sa i 10 d 20 0000 i 30 d 40 0000 el 2 Strb abcd bal 100 A bo 1 funcl Ox5fdf8 func2 0x610d8 main 0X616a8 157 Figure 7 6 Output from executing prog4 prog4 ch CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES is shown in Figure 7 6 When the script in Program 7 10 is processed a block end event in the function funcl will trigger the callback The first two blocks in the output in Figure are similar to ones in Figure 7 2 Instead of displaying the address of each variable in a given stack level in Figure 7 2 the output in Figure 7 6 displays the value of a each variable At the stack level 3 there are 75 global variables The output for a large nu
363. s for Embedding Ch lt embedch h gt Ch StackName Ch StackName Synopsis include lt ch h gt char Ch StackName ChlInterp t interp int level int i sfunc char classname Purpose Obtain the name in a stack Return Value If the function is successful it returns the name in the stack If the specified stack level is invalid beyond its range or the program is executed at the top level it returns NULL Parameters interp Ch interpreter level The level of the stack isfunc It is 1 if the stack is a function it is 2 if the stack is a member function contructor or destructor else it is 0 classname It is NULL if the stack is not a member function Description Function Ch StackName obtains the name in the stack The second argument level specifies the level of the stack Level 0 is the currently executed function whereas level n 1 is the function that has called level n The third argument determines if the stack is a program function or member function including constructor and destructor The program scope outside any function is the top level For example when function 1eve10 is being called in program proc c it has level 0 Function 1eve11 which calls function 1evel10 has level 1 Function level2 which calls function 1evel11 in turn has level 2 Program prog c has the stack level 3 File name prog c void level2 int i2 20 levell void levell
364. s of arg 5 of f5 is 2147483647 number of elements of arg 6 of f5 is 2147483647 number of elements of arg 7 of f6 is 4 number of elements of arg 1 of f8 is 6 number of elements of arg 2 of f8 is 1 number of elements of arg 3 of f8 is 1 dim of arg 1 of fl is 0 dim of arg 4 of fl is 0 dim of arg 1 of f2 is 0 dim of arg 2 of f4 is 2 dim of arg 3 of f4 is 2 dim of arg 4 of f4 is 1 dim of arg 5 of f4 is 1 dim of arg 2 of f5 is 2 dim of arg 3 of f5 is 2 dim of arg 4 of f5 is 1 dim of arg 5 of 5 is 2147483647 dim of arg 6 of 5 is 2147483647 dim of arg 7 of f6 is 1 dim of arg 1 of f8 is 2 dim of arg 2 of f8 is 2 dim of arg 3 of f8 is 2 Extent 1 of arg 1 of fl is 0 Extent 1 of arg 4 of fl is 0 Extent 1 of arg 1 of f2 is 0 Extent 1 of arg 2 of f4 is 2 Extent 2 of arg 2 of f4 is 3 Extent 1 of arg 3 of f4 is 2147483647 Extent 1 of arg 4 of f4 is 2147483647 Extent 1 of arg 5 of f4 is 2147483647 Extent 1 of arg 2 of 5 is 2 Extent 2 of arg 2 of 5 is 3 Extent 1 of arg 3 of 5 is 2147483647 Extent 1 of arg 4 of 5 is 2147483647 Extent 1 of arg 5 of 5 is 2147483647 Extent 1 of arg 6 of 5 is 2147483647 Extent 1 of arg 7 of f6 is 4 Extent 1 of arg 1 of f8 is 2 Extent 2 of arg 1 of f8 is 3 Extent 1 of arg 2 of f8 is 2147483647 Extent 2 of arg 2 of f8 is 2147483647 Extent 1 of arg 3 of f8 is 2147483647 Extent 2 of arg 3 of f8 is 2147483647 arg 1 of fl is function 0 arg 4 of fl is function 0 arg 1 of f7 is functio
365. s so that C borland bcc55 bin make exe from Borland will be used in Ch shell path stradd C borland bcc55 bin path This statement can be placed in the startup file chrc in the user s home directory For the convenience of users command ch d copies a startup file from the directory CHHOME config to the user s home directory Header files ch h and embedch h located in the directory CHHOME extern include are needed to compile code using Embedded Ch Ch libraries chsdk 1iband embedch 1ib are used to create dynamically loaded libraries using Visual NET or VC Using a Borland compiler libraries chsdk bc 1iband embedch bc lib instead of chsdk 1ib and embedch lib shall be used These libraries are located in the direc tory C Ch extern lib 1 2 6 Building Executables in Windows Using Borland CBuilder Executable binary programs and dynamic loaded libraries in Windows can also be built using Borland CBuilder version 6 or above An example using Borland CBuilder is are distributed in Embedded Ch SDK in the directory CHHOME toolkit demos embedch Borland CBuilder You may click or type CHHOME toolkit demos embedch Borland CBuilder embedfuncmain bpr to start the project for compilation and linking More information can be found in CHHOME toolkit demos embedch Borland CBuilder readme txt 1 3 Other Essential APIs and Options for Embedded Ch When an embedded Ch program is executed it first will be parsed then executed
366. sis include lt embedch h gt int Ch AppendRunScript ChInterp t interp const char code Purpose Append a fragment of the Ch code to an existing Ch program inside the interpreter executed by functions Ch RunScript or Ch ParseScript and Ch ExecScript The Ch code will be parsed and executed Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter code A fragment of the Ch code to be appended to the existing Ch program inside the interpreter Description When a program is run in Ch it first will be parsed then executed The process is similar to compile and execution using a C compiler Like Ch RunScript Ch AppendRunScript performs these two steps together for a program or functions passed through its argument The function Ch AppendRunScript executes a fragment of Ch program from C space The variable code contains the fragment of the Ch code Restrictions 1 The code passed can process the preprocessing directive include in the code Some preprocessing directives such as define have to be handled separately from other code For example Ch AppendRunScript interp define A 2 All preprocessing directives can exist in the code passed by function Ch RunScript Ch ParseScript Ch AppendRunScriptFile or Ch AppendParseScriptFile 2 The code passed cannot contain comments delimitered by such as this is comment If the code passed is l
367. ss pointed to by pt r The return value is saved in variable ret val_g and the integer 10 is passed as the argument In the same way the function call below Ch CallFuncByAddr interp fptr amp retval_g 20 calls the same Ch function with argument of 20 at the second time The output from executing the file built from funcal12 c is shown in Figure 3 2 A Ch function in a Ch program can also be called by its address with API Ch CallFuncByAddrv The prototype of API Ch CallFuncByAddrv is given in header file ch h as follows int Ch CallFuncByAddrv ChInterp t interp void fptr void retval va list ap where ap is the variable argument list of a function obtained by va start Details about Ch CallFuncByAddrv can be found in Ch SDK User s Guide 3 3 Calling Ch Functions by Name Using Ch CallFuncByName and Ch CallFuncByN In addition to Ch CallFuncByAddr API Ch CallFuncByNamew can also be used to call a Ch function in the Ch program In this case the Ch function is called by its name instead of the address The prototype of API Ch_CallFuncByName is given in header file ch h as follows int Ch CallFuncByName ChInterp t interp char name void retval The first argument is an instance of Ch interpreter The second argument is a string containing the name of the function The function name can be a function located in a function file specified by the function file path fpath For a generic function its regu
368. st h VarArgsDelete interp arglist h End interp Oaaaaaaa CY GGA SAG SAMA GA CO Ov Or OO 57 Program 3 8 Calling Ch function with API Ch CallFuncByNameVar callfuncbynamevar c CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 5 CALLING CH FUNCTIONS BY CH CALLFUNCBYNAMEVARO include lt array h gt include lt stdio h gt int n ch 2 ch m 3 p_ch amp n ch double f ch 10 0 int a ch 2 3 1 2 3 4 5 6 int b ch 2 3 1 2 3 4 5 6 array int c ch 2 3 1 2 3 4 5 6 array int d ch 2 3 1 2 3 4 5 6 struct tag int i double f soch 10 20 sp_ch amp s_ch int func int n int m double f int p int a 2 3 int b n m array int c 2 3 array int d n m struct tag s struct tag sp printf func called Xn s printf n Sd n n printf m Sd n m printf f Sf n f printf p d n p printf a 1 1 d n a 1 1 1 I printf b 1 2 d n a 1 2 printf c n d c printf d n d d priftk s i dXn s 1 printf sp f Sf n sp gt f return 0 int main func n ch m ch f ch p ch ach b ch c ch d ch s ch sp ch return 0 y Program 3 9 Ch program with function func called by Ch CallFuncByNameVar callfuncby namevar ch 58 CHAPTER 3 CALLING CH FUNCTION
369. status CH ERROR printf Error parsing program embedch2 ch failed n status Ch ExecScriptM interp argv2 0 if status CH ERROR printf Error executing program embedch2 ch failed n release memory and terminate the interpreter Ch End interp Program 1 8 A program executing multiple embedded Ch programs embedchprograms c 16 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS S EEK k k kok k k kok k k kok k kok k kok k kok kk kok k oko kok kok kk k ck k ck k kc k kCk Ck k kc k k ck k ck ck ck ck ck ck ckck ck ckck ck ck ck kk File Name thread c executing multiple Ch programs using multiple instances of Ch interpreter CKCKCKCKCkCkCKCKCKCkCKCkCKCkCKCKCKCkCKCKCKCkCKCKCKCKCK KA KA KCKCKCKCKCKCKCk k k k k k k k kc kck ck k ck ck ck k k k k o sk d e kx k finclude embedch h finclude lt stdio h gt finclude lt string h gt int main ChInterp t interpl interp2 int status retval char argvl embedchl ch NULL char argv2 embedch2 ch opt1 opt2 NULL initialize an embedded Ch interpl Ch Initialize amp interpl NULL execute embedchl ch status Ch RunScriptM interpl argvl if status CH ERROR printf Error execution of embedchl ch in interpl failed n execute embedch2 ch status Ch RunScriptM interpl argv2 if status CH ERROR printf Error execution of embedch2
370. stem function in the Ch space prog3 c 145 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK int i 40 double g 100 double a 5 1 2 3 4 5 int main int i 10 x sysfunc i return 0 Program 7 7 Ch program uses the system function sysfunc prog3 ch event MASKCALL is true The stack level is O0 The current line number is 0 Function sysfunc starts at line 0 Function sysfunc ends at line 0 The function name is sysfunc The source code of the string is int sysfunc int i The total number of symbols is 2 symbol index 0 name chretval address 53640 symbol index 1 name i address 59058 i 10 he stack level is 1 The current line number is 8 Function main starts at line 5 Function main ends at line 10 The function name is main The current file name is prog3 ch The total number of symbols is 3 symbol index 0 name _ func address 554d0 symbol index 1 name i address 58fd8 symbol index 2 name x address 59008 i 10 The stack level is 2 The current line number is 0 The current file name is prog3 ch The total number of symbols is 4 symbol index 0 name i address 53880 symbol index 1 name g address 5c1c0 symbol index 2 name a address 473f0 symbol index 3 name main address 57110 i 40 i in sysfunc chdl is 10 i in sysfunc is 10 Figure 7 5 Output from executing prog3 c 146 CHAPTER 7 TH
371. t strdup token do process the rest token of the command line token strtok NULL t tab and space as delimiters if token NULL if token start strl str2 str3 strcpy buf token 1 kickout and get stri token strtok NULL if token NULL not start str or start str strcat buf Nt is treated as space strcat buf token get strl str2 if token strlen token 1 NO amp amp not finished token strlen token 1 7 amp amp not space token strlen token 1 t not tab token strtok NULL t strcat buf token get strl str2str3 token buf argv i strdup token while token amp amp i lt MAX NUM ARGS return 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 195 Chapter APIs for Embedding Ch lt embedch h gt File Name callback c include include include include include lt stdio h gt lt string h gt lt stdlib h gt lt embedch h gt debug h void chCallback ChInterp t interp ChBloc debugInfo t dgptr breakPoint t bp void varptr size t size dgptr bp debugInfo t clientdata dgptr gt calldata calldata dgptr gt level calldata gt level dgptr gt bkpt reset a called function with a bre k t calldata Ch
372. t Embedded Ch SDK APIs for the same embedded Ch can be invoked across different threads with the same first argument of interp A running thread of a Ch script can be aborted in a thread by the function Ch Abort in another thread Check Appendix A on Ch Abort and Ch SetGlobalUserData for more examples about embedding Ch in multi thread applications across different platforms in Windows and Unix 19 CHAPTER 1 EMBEDDING CH PROGRAMS IN C SPACE 1 5 EMBEDDING CH IN MULTI THREAD APPLICATIONS KK kk kk kk kA kk kk kk kk kk kCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCKCkCk k k k kCk k k k k k k KCk k Ck k ck k k k ck k ck ck ck ok kk File Name threads c Embeddeding Ch in a multi thread application using POSIX pthread OKCKCKCKkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCkCKCk Ck k k k k kc k kk k k k k k k kk include lt stdio h gt include lt stdlib h gt include lt embedch h gt include lt pthread h gt define N 3 char code int func int i printf Sd hello n i return 0 void thread_function void arg ChInterp t interp int val retval val int arg Ch Initialize amp interp NULL Ch AppendRunScript interp code Ch CallFuncByName interp func amp retval val Ch End interp return NULL int main pthread t threadId N int i arg N status Create thr new threads for i 0 i lt N itt arg i itl
373. t2 cpp n i J return jti Program 5 38 Ch Program script2 cpp m il in Classl Classl 1 m il in Classl memfunl 11 m il in Classl memfunl 21 i 10 j 21 in func in script2 cpp retval in prog2 cpp 31 m il in Classl Classl 21 Figure 5 5 Output of executable compiled from Program 109 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 6 INVOKING C CLASSES IN THE MAIN PROGRAM FROM CH SPACE WITH DL FILE 5 6 Invoking C Classes in the Main Program from Ch Space with dl File In the example in the previous section file sampclass_chdl c with interface functions shown in Pro gram 5 31 is compiled and linked with the application program without a separate dynamically loaded library dl file An alternative method with a dynamically loaded library dl file has been used in the past Although this method has been deprecated it is described in this section using the same example presented in the previous section Because this example uses the same functions in the previous section we only list programs that are different from the ones in the previous example In this example C header file in Program 5 29 interface file sampclass chdl c Chscript script ch application program prog cpp are the same as before To export a member function for callback from Ch scripts the member function shall be defined and pro totyped with the type qualifier EXPORTCHCLASS as shown in Program 5 39 for header file samp
374. tacks watch expr add an expression into the watch list remove expr remove an expression from the watch list remove remove all expressions from the watch list stopat filename cond set a new breakpoint in a file at line stopin funcname cond set a new breakpoint in a function stopvar varname cond set a new breakpoint for a controlling variable clearline filename clear a breakpoint in a file at line clearfunc funcname clear a breakpoint for a function clearvar varname clear a breakpoint for a variable clear clear all breakpoints help display this debug menu assign var expr assign a value to a variable call func call a function print expr print out the value of an expression expr print out the value of an expression abort abort the debugger debug gt Figure 7 10 The menu for the user interface of the debug program 7 4 A Sample Debugger Based on the debug and callback interface described in the previous sections a text based debugger is presented in this section Using the concept illustrated in this example one may develop sophisticated GUI based debuggers or remote debuggers This debug program has all capabilities available in a typical debugger for binary C programs The interactive user interface for our debugger is shown in Figure The menu on the left before a colon shows a command and the description on the right explains the action taken for the command With this debugger the user can load a program start the
375. tart ap VA NOARG funcl ap va end ap return 0 62 Program 3 10 Calling Ch functions taking variable number of arguments callchvna ch CHAPTER 3 CALLING CH FUNCTIONS FROM C SPACE 3 6 CALLING CH FUNCTIONS WITH VARIABLE NUMBER OF ARGUMENTS include lt stdio h gt include lt embedch h gt int main ChInterp t interp char argvv callchvna ch NULL int retval status int i 5 int j 10 float f 20 double d 40 ChVaList t ap ch initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program callchvna ch int funcl va list ap printf calling funcl ap ch Ch VarArgsAddArg interp Ch VarArgsAddArg interp Ch CallFuncByName interp Ch VarArgsAddArg interp Ch CallFuncByName interp Ch VarArgsDelete interp int func2 int i printf Xncalling func2 ap ch Ch VarArgsAddArg interp Ch VarArgsAddArg interp Ch CallFuncByName interp Ch VarArgsAddArg interp Ch CallFuncByName interp Ch VarArgsDelete interp int func3 printf Xncalling func3 ap ch h VarArgsAddArg interp h VarArgsAddArg interp h CallFuncByName interp h VarArgsAddArg interp h CallFuncByName interp OX C C C C C Q h VarArgsDelete interp End interp Ch CallFuncByNameVar interp ky kap ch kap ch tuncl ka
376. tents extl ext2 and ext 3 are added after the argument aa2 It is shown as follows dimension and extents of aa2 dim 3 extl 2 ext2 3 ext3 4 Ch CallFuncByName interp sumNd sum ret aa2 dim extl ext2 ext3 The output from executing this example is shown in Figure 6 2 6 1 2 Calling Ch Functions with Return Type of Computational Array In this section we will describe how to call Ch functions that return computational arrays Ch Functions with return type of computational array can be called in C by one of functions Ch SymbolAddrByName Ch CallFuncByAddr Ch CallFuncByName The second argument of these functions shall be the the address that will hold the returned array The calling syntaxes for functions returning a fixed length array and variable length array are the same Program 6 6 contains two functions funcl and func2 Function funcl returns a computa tional array of fixed length whereas Function funcl returns a computational array of variable length After program returnarray chis loaded these two functions in the Ch space are called in the C space by function calls of Ch CallFuncByName interp funcl a and Ch CallFuncByNameVar interp func2 b arglist respectively Function func1 has no argument The argument list for the extents of the variable length array of function unc2 is built with values in the C space using API Ch VarArgsAddArg in the foll
377. ter to function it is 0 Field isconstructor is 1 if the member is the constructor of a class Otherwise it is 0 Field isdestructor is if the member is the destructor of a class Otherwise it is 0 Field isvararg is l if the member is a function type and its argument contains a variable number of arguments For example for members func1 and func2 the value for isvararg is 1 class tag int unci ant ip u int func2 int j Field arraytype is not 0 if the member is an array type Similar to the return value of func tions Ch ArrayType and Ch_FuncArgArrayType It contains one of the following macros defined in header file ch h Macro Description Example CH_UNDEFINETYPE not an array intri CH CARRAYTYPE C array int a 3 CH CARRAYVLATYPE C VLA array int a n int func int a n int b int c amp CH CHARRAYTYPE Ch array array int a 3 CH CHARRAYPTRTYPE pointerto Ch array array int ap 3 CH CHARRAYVLATYPE Ch VLA array array int a n int fun array int a n array int b array int c amp A poiner to C array is not considered as an array for this member arraytype Field dim gives the dimension of array if the member is a C array or Ch computational array Field extent contains the extent for each dimension of array if the member is a C array or Ch computational array Only arrays of dimension up to 7 can be handled For example for member a in the structure tag struct tag int a 3 4
378. terp Ch End interp printf N var nsf var Program 2 13 Removing and declaring variables in the Ch space removevar c int var 10 b 20 printf var in removevar ch d n var Program 2 14 A Ch program to test removing and declaring variables removevar ch Ch DeclareVar interp int s A system variable can be changed to a new data type specifier using the API Ch DeclareTypedef For example the typedefed specifier newType t of int type is used to declare a system variable sysvar Ch DeclareVar interp int newType t Ch DeclareTypedef interp newType t Ch DeclareVar interp newType t sysvar In some applications it may be desirable to reuse some variables in the Ch space which are already declared Global and system variables including variables of function type in the Ch space can be removed by preprocessing directive pragma For example the following Ch statement pragma remvar var removes the variable var of the global scope or system scope declared with the type specifier declspec global in a Ch program After a variable is removed from the symbol table its name can be re used and redeclared as any other data type For example when the Ch script Program loads and executes Program 2 14 using the API Ch RunScript The variable var of int type is declared and initialized with value 10 Its value is then 45 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPAC
379. terp ChUserDefinedTag t udtag int in dex ChMemInfo_t meminfo Purpose Obtain the information for a member of variable of the user defined structure class or union type based on its index number in the symbol table for its type Return Value If the function is successul it return CH OK otherwise it returns CH ERROR Parameters interp A Ch interpreter udtag The tag for a user defined type a return value from the function Ch UserDefinedTag index The index number of the member in the symbol table for the user defined type meminfo A pointer to structure containing the information for a member of a user defined type Description This function Ch UserDefinedMemlInfoByIndex can be used to obtain the information for a member of variable of the user defined structure class or union type based on its index number in the symbol table for its type The function Ch UserDefinedMemInfoByName can be used to obtain the information for a member based on the member name Type ChMemlnfo t is a structure containing the information for a member of a user defined structure class or union type typedef struct ChMemInfo int index index number of the member char memname member name int offset offset of the member from the starting memory ChType t dtype data type of the member int ispublic 1 public 0 private int isfunc 1 function type 0 not function type int ism
380. terp udtag index amp meminfo printMemInfo amp meminfo addr i udinfo size Program 2 9 C program for obtaining information for variables of user defined type userdefinedsymbol c 38 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 4 EVALUATING EXPRESSIONS for index 0 index lt udinfo totnum index t Ch UserDefinedMemInfoByIndex interp udtag index amp meminfo printf meminfo offset d n meminfo offset Ch UserDefinedMemInfoByName interp udtag i amp meminfo printf i Sd n int addr meminfo offset Ch UserDefinedMemInfoByName interp udtag d amp meminfo printf d Sf n double addr meminfo offset Ch UserDefinedMemInfoByName interp udtag p amp meminfo printf p Sp n int addr meminfo offset Ch UserDefinedMemInfoByName interp udtag a amp meminfo printf a 0 Sd n int addr meminfo offset return 0 int printMemInfo ChMemInfo t meminfo void addr printf meminfo gt index dWMn meminfo gt index printf meminfo gt memname s n meminfo gt memname printf meminfo gt offset dWMn meminfo gt offset if meminfo gt dtype CH INTTYPE printf meminfo gt dtype CH INTTYPE 1 n lse if meminfo gt dtype CH DOUBLETYPE printf meminfo gt dtype CH DOUBLETYPE 1 n lse if meminfo gt dtype CH INTPTRTY
381. the initialization in the Ch space Description A C program has global variables Global variables in a Ch script loaded by Ch_ParseScript Ch RunScript Ch_RunScriptMO as well as Ch AppendParseScript Ch AppendParseScriptFile Ch AppendRunScript Ch AppendRunScriptFile in an instance of Ch engine will be removed when a new Ch program is loaded In addition to global variables a Ch environment contains system variables such as path and fpath The system variables are persistent When a new program is loaded these system variables remain in the system Functions Ch AppendParseScript and Ch AppendRunScript can be used to declare variables of global scope like Ch AppendParseScript interp int a b Function Ch DeclareVar can be used to declare system variables of valid data type in Ch including simple type pointer typer struct and class type function type etc The type qualifier decl spec global is added at the beginning of the declaration statement of function Ch DeclareVar The statement Ch DeclareVar interp int a b is equivalent to declare system variables a and b in the Ch space as follows declspec global int a b Because type specifiers declspec global and typedef cannot be used together to declare a variable a new data type can be created by using both Ch DeclareVar and Ch DeclareTypedef together as shown below Ch DeclareVar interp int newtype t Ch DeclareTypedef
382. then recursively prints out values for variables in the calling functions Finally it prints out the values for global variables For a variable of class structure union type the name and value for each member of the object will be printed out For a member of class structure and union type it will be processed recursively The program can also handle scripts using classes and nested functions Program 7 8 lists the names and values of all local variables of the active function and variables in its upper level stacks Function main in Program is the same as one in Program 7 3 Function callback in Program is the same as one in Program except that it calls function chPrintSymbolValuesInStack in Program instead of symbols to pring symbols and their corresponding values recursively for all valid stack levels using a while loop Function chPrintSymbolValuesInStack call chPrintSymbolValue in a for loop to print each symbol and its value in a symbol table Program 7 9 is also used in a sample debugger program presented in the next section Like symbols function chPrintSymbolValuesInStack obtains the total number of variables in the symbol table for a given stack level Each variable in the symbol table is then processed in a for loop Functions Ch SymbolNameByIndex and Ch SymbolAddrByIndex give the name and address of a variable according to its index number in the symbol table The data type of the variable is obtained by the f
383. tion argument after the embedded Ch program has been executed Section 4 2 describes the method to export or access variables in C space from an embedded Ch program Section 4 3 describes the method to export or access variables in C space from an embedded Ch program using a dynamically loaded library 4 1 Accessing C C Variables from Ch Space Through Function Argu ments The memories of scalar variables arrays and structures in C space can be seamlessly shared in Ch space and can be achieved by just simply passing the address of the memory through function calls The concept of sharing data in C and Ch spaces through a function argument will be described through an example in this section Program 4 IJin C space has variables c_d of double type and c_data of an int array of 10 elements This C program invokes the Ch Program 4 2 with embedded Ch using function Ch RunScript and then calls Ch function chfun by its name using function Ch CallFuncByName The memory addresses for variables c_d and c data in C space are passed to function chfun in Ch space as arguments The function ch fun has no return value The output from executing Program A 1 is shown in Figure 4 1 In this example the data and memory in C space are shared in Ch space after the embedded Ch program has been executed Using the same technique the data and memory in Ch space can also be shared in C space 66 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4
384. tmpl NULL return 0 else if strcmp tmpl gt u file filename filename 84 tmpl gt u file linenum linenum dgptr gt bkpt tmpl gt next TE tmpl u file filename if tmpl condexpr free tmpl condexpr if tmpl condvalue free tmpl condvalue free tmpl else while tmpl gt next if strcmp tmpl next u file filename filename amp amp tmpl gt next gt u file linenum linenum tmp2 tmpl gt next tmpl gt next tmpl gt next gt next EL tmp2 gt u file filename if tmp2 gt condexpr fr tmp2 condexpr if tmp2 gt condvalue free tmp2 condvalue free tmp2 return 0 tmpl tmpl gt next return 0 remove a breakpoint from the list for a function int chClearBreakPointFunc debugInfo t dgptr char funcname breakPoint_t tmpl tmp2 tmpl dgptr gt bkpt if tmpl NULL return 0 else if strcmp tmpl gt u func funcname funcname dgptr gt bkpt tmpl gt next free tmpl gt u func funcname if tmpl condexpr free tmpl condexpr if tmpl condvalue free tmpl condvalue free tmpl Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 187 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER else while tmpl next if strcmp tmpl gt next gt u func funcname funcname tmp2 tmpl gt
385. trcmp name __class__ strcmp name __class_func__ strncmp name Ch 4 amp amp displayAll printf 16s name udtag Ch UserDefinedTag interp name dim Ch ArrayDim interp name n Ch ArrayExtent interp name 0 if dim 1 for i 0 i n i if datatype CH CHARTYPE amp amp char addr 0 break skip the rest characters of s 10 abc size printValue interp datatype addr udtag nestlevel addr char addr size if datatype CH CHARTYPE printf else if dim 2 m Ch ArrayExtent interp name 1 for i 0 i lt n i if i 0 p imtt 5108 for j 0 j lt m j size printValue interp datatype addr udtag nestlevel addr char addr size prrutf if i n 1 printr Xn print nM Program 7 9 Functions for printing values Gall variables in a stack stack c Contd CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES else if strncmp name Ch 3 se displayAll suppress Chstdio handle etc printf 16s name if addr NULL for extern int i addr is NULL udtag Ch_UserDefinedTag interp name printValue interp datatype addr udtag nestlevel printf n int printUserDefined ChInterp_t interp ChType_t datatype void addr ChUserDefinedTag_t udtag int nestlevel int i j size 0 indent index C
386. turn retval int chExpr debugInfo_t dgptr char expr ChInterp t interp ChValueNode t valuenode void addr int retval 0 interp dgptr gt interp valuenode Ch ExprValue interp expr amp addr if valuenode NULL if Ch DataType interp xpr CH VOIDTYP chPrintSymbolValue interp expr addr Ch DeleteExprValue interp valuenode 1 else retval 1 return retval int chAbort debugInfo t dgptr int i 1 if dgptr gt interp Ch Abort dgptr gt interp chCleanDebug dgptr exit 0 Program 7 16 Functions corresponding to the debug user interface menu debug c Contd 190 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 4 A SAMPLE DEBUGGER process the user input interactively This action menu is best implemented using a GUI int chProcessActionMenu debugInfo t dgptr action t act int abort char name MAX PRO Ii FALSE while abort FALSE getActionMenu dgptr switch act act case ACTION_L pir chLoadFile break case ACTION_S ptr chStart dgp abort TRU break case ACTION_R chRun dgptr abort TRU break PT STR nenum OAD dgptr TART Lr ES UN a case ACTION S chStepInto abort break TRUE condtrue TRUE ptr TEPINTO dgptr B case ACTION S chStepOver abort TRU break case ACTION C chContinue
387. turning type of void only the function is called The sample debugger can debug multiple programs one at a time The command abort terminates the program The header file debug h in Program defines macros structures and function prototypes for the debug program This sample debugger does not use a global variable Therefore it can be conve niently extended for debugging multi thread applications with multiple instances of Embedded Ch Boolean values TRUE and FALSE are defined for handling variables with only true or false values The macro MAX PROMPT STR is used to allocate memory for a string containing the user input at the prompt debug gt The macro MAX NUM ARGS is used for handling command line arguments which will be described later The enumerate type act ion t contains enumerate numbers for each command in the user interface menu The enumerate type runmode t contains enumerate numbers for each command that can execute a Ch script They include start run step next andcont The structure breakPoint t is for a linked list of breakpoints It can be used to hold any of three types of breakpoints of file function or variable type The member bt ype is used to hold the breakpoint type A union inside the structure contains the information for a breakpoint of different types A break point of file type needs both file name filename and line number 1inenum A breakpoint of function type needs a function name funcname and field called The p
388. two variables in C space will be exported and shared by embed ded Ch programs They will be used to illustrate two methods of handling global variables C header file expvar c h in Program 4 3 interface functions in Program 4 4 and application program 4 5 will be com piled and linked to create the executable program prog exe using a makefile in Program 4 6 and Program 4 7 for Unix and Windows respectively 68 CHAPTER 4 ACCESSING C C VARIABLES FROM CH SPACE 4 2 ACCESSING VARIABLES IN A HEADER FILE IN C SPACE FROM CH SPACE ifndef OEXPVAR H define EXPVAR H global variables to b xported to the Ch space const int il 100 int i2 200 i2 will be changed during execution function prototypes PP A fendif Program 4 3 C header file containing global variables expvar c h include lt ch h gt include lt stdio h gt include expvar c h EXPORTCH void expvar chdl void varg ChInterp t interp ChVaList t ap Tnt Las int ii2 printf The variables in the C space are d and dWMn il i2 Ch VaStart interp ap varg iil Ch VaArg interp ap int iil il pass the value to the Ch space ii2 Ch VaArg interp ap int ii2 amp i2 pass the address to the Ch space Ch VaEnd interp ap return EXPORTCH void changeCVar chdl ChInterp t interp i2 400 change value of C variable printf The variables in the C space are d a
389. uble retval funcl x Ch VaEnd interp ap return retval EXPORTCH double func2 chdl void varg ChInterp t interp ChVaList t ap double x double y double retval Ch VaStart interp ap varg x Ch VaArg interp ap double y Ch VaArg interp ap double retval func2 x y Ch VaEnd interp ap return retval Program 5 7 chdl file sample chdl c 83 CHAPTER 5 CALLING C C FUNCTIONS FROM CH SPACE 5 1 CALLING C FUNCTIONS IN THE MAIN PROGRAM FROM CH SPACE include lt stdio h gt include lt embedch h gt include sample h char code include sample h X double v 4 double d d func2 3 v int main ChInterp t interp int status double pd initialize embedded Ch Ch Initialize amp interp NULL run a Ch program status Ch AppendRunScript interp code if status printf Error execution of code failed n pd Ch SymbolAddrByName interp d printf d in the Ch space f n pd Ch End interp Program 5 8 C program invoking scripts in the memory progl c Program 5 1 invokes script program ch in Program 5 2 However scripts in the Ch space can also be invoked in the memory in the C space without a separate script file Program 5 8 invokes the script below include sample h double v 4 double d d func2 3 v directly in the memory by Ch AppendRunScript Like Program 5 2 function
390. unction Ch DataType If the data type is of structure class or union type the user defined tag information for the variable is obtained by the following statement udtag Ch UserDefinedTag interp name Otherwise the value for udtag is NULL The tag information udtag will be used to obtain the infor mation for the user defined type and its members later on The function Ch FuncType gives the func tion type of a variable If the variable is a function type of pointer to function or function its value will be printed out For function type of constructor destructor or member function only its defini tion is printed out Because header files may contain many function prototypes a variable of function prototype without function definition will be ignored in function chPrintSymbolValue Function Ch VarType can be used to test whether an identifier is a local or global variable It is used to test if a function is a local nested function in this example The global variable displayA11 in Program is a flag If it is set to 0 symbols for constructor destructor member functions internally declared variables func class class func handles for dynamically loaded standard libraries start ing with Ch such as Chstdio handle function dlclose and functions starting with dlclose 147 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 3 MANIPULATING LOCAL AND GLOBAL VARIABLES File Name prog4 c include
391. unctions with variable number of arguments The function func1 take a variable length argument list ap with type of va list The function func2 take variable number of arguments but at least one argument for example i need to be passed in when this function is called The function func3 take variable number of arguments and it can even have no argument at all The former two functions are also valid in C whereas function func3 is only valid in Ch This format allows a function to take any number of arguments without constraint on the first argument More information on how this format for handling function with a variable number of arguments can be found in Ch User s Guide The definitions of these three functions are shown in Program 3 10 The APIs va start va end va count and va elementtype are used to handle the variable length argument list These APIs are described in details in The Ch Language Environment Reference Guide To call these Ch functions from the C space with embedded Ch we need to create Ch style variable length argument lists in the C space first then pass these lists to Ch functions by calling Ch CallFuncByName Ch CallFuncByNameVar or Ch CallFuncByAddr The C program with embedded Ch to call Ch programs taking variable number of arguments are shown in Program 3 TI where APIs Ch VarArgsCreate Ch VarArgsAddArg Ch VarArgsAddArgVar and Ch VarArgsDelete are used to create Ch style variable length ar
392. ure class or union by Ch UserDefinedInfo typedef struct ChUserDefined ChType t dtype char tagname int size int totnum ChUserDefinedInfo t user defined data type Info CH UNIONTYPE tag name size of class struct union total number of members in class struct union or CH UNDEFINETYPE CH STRUCITYPE CH CLASSTYPE Type ChMemlnfo t is a structure used to obtain the information for a member of a user defined structure class or union by Ch UserDefinedMemlInfoByIndex and Ch UserDefinedMemInfoByName typedef struct ChMemInfo index number of the member in ch in Ch in in in in t index ar memname t offset t isfunc Type t dtype t ispublic t ismemberfunc member name offset of the member from the starting memory data type of the member 1 public 0 private f function type 0 not function type member funct 0 not member funct constructor 0 not constructor t isconstructor 200 Chapter A APls for Embedding Ch lt embedch h gt int isdestructor int isvararg int arraytype int dim int extent 7 int isbitfield int fieldsize int fieldoffset ChUserDefinedTag t udtag ChMemInfo t Functions 1 1 not destructor funct with variable num arguments 0 one of array types array dim ext
393. ure 2 5 35 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 3 OBTAINING INFORMATION FOR A VARIABLE OF STRUCTURE CLASS AND UNION File Name setvar2 c include lt stdio h gt include lt embedch h gt int main ChInterp t interp int status int c i c data 10 1 2 3 4 5 6 7 8 9 10 char argvv setvar ch NULL initialize embedded Ch Ch Initialize amp interp NULL run a Ch function file status Ch RunScript interp argvv if status CH ERROR printf Error execution of program setvar ch ch failed n c i 10 Ch SetVar interp ch i CH INTTYPE c i Ch ExprEval interp printf W ch i d n ch_i Ch SetVar interp ch p CH INTPTRTYPE amp c i c i 20 Ch ExprEval interp printf W ch i d n ch_i Ch ExprEval interp printf ch_p d n ch_p Ch_ExprEval interp ch_p 30 printf c i din c i Ch SetVar interp ch data CH INTPTRTYPE amp c data Ch ExprEval interp printf ch_data 9 d n ch_data 9 Ch CallFuncByName interp chfun NULL Ch ExprEval interp printf ch_data 9 d n ch_data 9 Ch End interp Program 2 6 Assigning values in C space to variables in a script in Ch space setvar2 c include lt stdio h gt int ch_i ch_p ch_data void chfun printf in the Ch function chfun ch data 0 d ch data 9
394. used to indicate a variable of global scope by Ch VarType interp name Function Ch VarType can be used to test if a function is a nested local function 316 Chapter A APIs for Embedding Ch lt embedch h gt Ch VarType Example See Program 3 Program 7 8 Ch GlobalSymbolAddrByIndex See Also Ch DataType Ch ArrayDim Ch ArrayExtent Ch ArrayTypeO Ch_FuncType Ch FuncArgNum Ch IsFuncVarArg Ch UserDefinedTag Ch UserDefinedInfo 317 Appendix B Porting Code with Embedded Ch APIs to the Latest Version B 1 Porting Code with Embedded Ch APIs to Ch Version 6 0 1 The default home directory of Embedded Ch has been changed For the function call Ch Initialize amp interp NULL the default home directory for Embedded Ch is CHHOME toolkit embedch instead of CHHOME The dynamically loaded library for an instance of the interpreter is CHHOME toolkit embedch extern lib chmt1 dl etc instead of using usr lib chmtl dl etc in Unix and C Windows System32 chmt1 dl etc in Windows To load a dynamically loaded library 1ibsample dl when the program is parsed change void Chsample handle dlopen libsample dl RTLD LAZY to extern void Chsample handle dlopen libsample dl RTLD LAZY in your Ch script code Changed the handling of Ch SymbolAddrByName interp a for array int a 2 3 in Ch space to be consistent with handling of C array Change int
395. ut no generic functions It also contains tag names for classes structures and unions as well as names defined by typedef The index number for the symbol table starts with 0 GlobalSymbols in header files including system header files are treated the same as in a user s program Global variables qualified by declspec global are not included in the symbol table Example See Ch GlobalSymbolAddrByIndex See Program 2 4 See Also Ch GlobalSymbolIndexByName Ch GlobalSymbolAddrByNamej Ch_GlobalSymbolAddrByIndex Ch_GlobalSymbolNameByIndex Ch SymbolTotalNum Ch_VarType 282 Chapter A APIs for Embedding Ch lt embedch h gt Ch InitGlobalVar Ch InitGlobalVar Synopsis include lt embedch h gt int Ch_InitGlobalVar ChInterp_t interp int flag Purpose Enable or disable initialization of global variables at the parse time Return Value This function returns CH OK on success and CH ERROR on failure Parameters interp A Ch interpreter flag The flag to enable or disable initialization of global variables at the parse time If flag is true with the value of 1 it enables initialization of global variables at the parse time Otherwise if the flag is O it disables initialization of global variables at the parse time which is the default Description By default for a global variable declared with initialization the initialization is performed at the runtime after the entire program is parsed For a cl
396. ved by function Ch GlobalSymbolAddrByName or Ch SymbolAddrByName when there is no local variable in its scope The output from executing the file built from funcall c is shown in Figure Function Ch ExprCalc can also be used to calculate an expression with function calls The function definition in an expression can be located in a function file specified by the function file path _fpath For a generic function its regular function version with a function prototype will be used For example for function sin the regular function double sin double will be used if sin is an operand of an expression Variables and functions in the Ch space can also be added by APIs Ch AppendParseScript and Ch AppendRunScript without calling Ch RunScript first then used by APIs Ch ExprParse Ch ExprEvalO and Ch ExprCalc described in section 3 4 3 2 Calling Ch Functions by Address Using Ch CallFuncByAddr and Ch CallFuncBy A Ch function in a Ch program can also be called by its address with API Ch CallFuncByAddr The prototype of API Ch CallFuncByAddr is given in header file ch h as follows int Ch CallFuncByAddr ChInterp t interp void fptr void retval 49 CHAPTER 3 2 CA amp BLINNG HTEENNCIOSS FROM DISREGS USING CH CALLFUNCBYADDR AND CH CALLFUNCB YADDRV BORK kok kok kk kk kk kk kk kk kk kk kk kk kk kk KCK KCKCKCKCkCK kk k kck Kk k kc k k k k ck k ck k ck k ck ck ck ok koc File Name funcall2 c
397. vel 1 The third block with the stack level 2 is for the program The symbol table for a program does not contain system functions and variables defined by the APIs Ch DeclareFunc an Ch DeclareVar respectively 141 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK tag tag is called tag tag is called tag tag is called tag funcl called argument i in funcl is true event MASKEND is true The stack level is 0 The current line number is 33 Member function tag funcl starts at line 27 Member function tag funcl ends at line 35 Member function tag funcl Class name is tag The current file name is cplus cpp The total number of symbols is 12 symbol index 0 name i address 595a0 symbol index 1 name z address 595d0 symbol index 2 name __func__ address 3dde0 symbol index 3 name __class__ address 4f8b0 symbol index 4 name class_func__ address 511a8 symbol index 5 name i address 50b10 6 i 8 lt symbol index 6 name y address 50b14 symbol index 7 name tag address 56fa8 symbol index 8 name tag address 50b12 symbol index 9 name funcl address 50b12 symbol index 10 name this address 592f0 symbol index 11 name j address 59320 i 30 The stack level is 1 The current line number is 43 Function main starts at line 38 Function main ends at line 45 The function name is main The current file name is cplus cpp The total
398. will also contains members of the class The variables class and class func contains the class name and member function name respectively In addition it has a special this pointer to the instance of the class itself when a member function is called Inside the if statement block the value for the local variable i is 30 The second block in Figure 7 3 contains the information for the calling function main with the stack level 1 The third block in Figure 7 3 with the stack level 2 is for the program The symbol in the symbol table does not include the tag name for the class Ch supports nested functions Program 7 3 can also be used to process a Ch program using nested func tions Function nestedfunc in Program 7 5 contains a local function unc1 Function main calls function nestedfunc which in turn calls the local function func1 When the local func tion funcl is called inside function nest edfunc the end block of the if statement will trigger the callback function in Program 7 3 The output from executing the command prog nestedfunc ch is shown in Figure The first block in Figure contains the information for the active function funcl The second block in Figure 7 4 contains the information for the nesting function nest edfunc with the stack level 1 The third block with the stack level 2 is for the calling function main The last block with the stack level 3 is for the program Often time a developer may e
399. x interp index Ch_SymbolAddrByIndex interp index p int Ch SymbolAddrByName interp i if p NULL printf i is not available n n else printf i dinin p return 0 Program 7 3 C program which changes the function stack prog2 c Contd 137 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 2 CHANGING STACK of function to the calling function The member level of the argument of structure ChBlock t contains the level of the function or program Application of Ch ChangeStack is illustrated by Program Program 7 3 will access variables at different levels of function Unlike Program 7 2 the file name of a Ch script program executed by Pro gram 7 3Jis provided in a command line argument argv 1 Itis then passed to the function Ch RunScript so that Program 7 3 can execute different Ch scripts The callback does not have client data But the events to trigger the callback function is the same as in Program 5 1 The statement if calldata gt event amp CH MASKEND se strcmp calldata funcname funcl printCalldata calldata symbols interp while Ch_ChangeStack interp level amp calldata2 CH OK in the function callback inside Program 7 3 indicates that when it is triggered by a end block event by the active function funcl it will call the function printCalldata which is the same as in Program 7 2 to print the information for the active function
400. xpose binary C functions as system functions to Ch programs These system functions are declared in the Ch space by the API Ch DeclareFunc Program 7 6ldefines a system function sysfunc in the Ch space by the statement Ch DeclareFunc interp int sysfunc int i ChFuncdl t sysfunc chdl Functions printCalldata and symbols in Program are the same as ones in Programs 7 2 and 7 3 The conditional statement if calldata gt event amp CH_MASKCALL ss strcmp calldata funcname sysfunc enales the callback function to just print out the information for the active function sysfunc and its calling functions recursively when it is triggered by a call event Program 7 7 calls system function sysfunc The output from executing Program is shown in Figure 7 5 The first block in Figure 7 5 contains the information for the active system function sys func with the stack level O Instead of printing out the file name function printCalldata prints out the source code of a string for a system function prototype specified in the second argument of func tion Ch DeclareFunc The current line number is 0 when the program is executing a system function The symbol table contains the internal variable chretval and function argument i Inside function sysfunc the passed value from Program 7 7 for the argument of the function is 10 The second block in Figure 7 5 contains the information for the calling function main with the stack le
401. xprEval interp ii can be replaced by the following statements if Ch ExprParse interp ii Ch ExprEval interp ii else printf Error Ch expression ii is invalid n Ch_ExprEval has no way to return a value so a global variable such as ii or dd in the Ch space is used to hold the result of the expression first It is then accessed in the C space through its address using function Ch GlobalSymbolAddrByName or Ch_SymbolAddrByName Function Ch ExprCalc can obtain the result of an expression in the Ch space directly without through such an intermediate variable in the Ch space For example the statements strcpy expr 2 1 pow 2 0 3 cos 0 3 Ch_ExprCalc interp expr CH_DOUBLETYPE amp result in Program 2 12 evaluate the expression expr of 2 1 pow 2 0 3 cos 0 3 in the Ch space and pass its result through the fourth argument The data type of the fourth argument specified by the third argument of the function shall be compatible with the data type of the expression The output from executing the program built from in Program 2 12 is shown below 43 CHAPTER 2 ACCESSING CH VARIABLES FROM C SPACE 2 5 REMOVING AND REDECLARING VARIABLES BORK kok kok kk kk kk kk kk kk kk kk kk kk kk kk KCK kk kok kok kok kck kkk k kc I I OO HK File Name exprcalc c Evaluating expression in Ch space KCKCKCKCKCKCKCkCkCKCkCk kCKCkCkCkCKCKCkCkCKCKCkCkCKCKCk KCKCKCk KCKCkCk kk kok kk kk ck ck
402. y by function Ch AppendRunScript Either file name or string of the code is printed out by function printCalldata Ifthe code is from a string the source is a string up to 70 characters starting with 8st ring This prefix for the code of string is skipped in the output by the statement printf The source code of the string is s n calldata gt source strlen GGstring The output from executing Program 7 2 is shown in Figure 7 1 Because the callback function is called six times for the specified events function printCalldata prints out the information of the active 131 CHAPTER 7 THE DEBUG AND CALLBACK INTERFACE TO A CH PROGRAM 7 1 OBTAINING PROGRAM AND FUNCTION INFORMATION BY A CALLBACK FUNCTION File Name progl c include lt stdio h gt include lt string h gt include lt embedch h gt struct tag int i double d void callback ChInterp t interp ChBlock t calldata ChPointer_t clientdata int main ChInterp t interp char argvv progl ch NULL struct tag s 10 20 int mask count 0 ChPointer_t clientdata Ch Initialize amp interp NULL mask CH MASKCALL CH MASKRET CH MASKBLOCK CH MASKEND clientdata amp s Ch AddCallback interp mask callback clientdata count Ch RunScript interp argvv Ch End interp return 0 int printCalldata ChBlock_t calldata callback at the
403. y function Ch FuncArgArrayExtent Example See Ch FuncArgDataType See Also Ch FuncArgDataType Ch FuncArgArrayExtent Ch FuncArgArrayType Ch FuncArgIsFunc Ch FuncArgIsFuncVarArg Ch_FuncArgFuncArgNum Ch_FuncArgUserDefinedName Ch_FuncArgUserDefinedSize 257 Chapter A APIs for Embedding Ch lt embedch h gt Ch FuncArgArrayExtent Ch FuncArgArrayExtent Synopsis include lt ch h gt int Ch_FuncArgArrayExtent ChInterp_t interp const char funcname int argnum int dim Purpose Obtain the number of elements in the specified dimension of for an argument of array type for a function of global variable in the Ch space Return Value If the argument for a function of global variable is of array type This function returns the number of ele ments in a specified dimension of the array Otherwise it returns 0 Parameters interp A Ch interpreter funcname Name of the function in a global variable argnum The argument number in the argument list of the function O for the first argument dim An integer specifying for which dimension the number of elements will be obtained 0 for the first dimension Description This function returns the number of elements in the specified dimension of an argument of array type of a function in the Ch space For array a in int func int a n m lf 0 is passed to this function as the fourth argument the extent of the first dimension i e n will be returned Otherwise if
404. y merchantability or fitness for a particular purpose all of which are expressly disclaimed Users should be aware that included in the terms and conditions under which SoftIntegration is willing to license the Ch lan guage environment as a provision that SoftIntegration and their distribution licensees distributors and dealers shall in no event be liable for any indirect incidental or consequential damages in con nection with or arising out of the furnishing performance or use of the Ch language environment and that liability for direct damages shall be limited to the amount of purchase price paid for the Ch language environment In addition to the foregoing users should recognize that all complex software systems and their doc umentation contain errors and omissions SoftIntegration shall not be responsible under any circum stances for providing information on or corrections to errors and omissions discovered at any time in this documentation or the software it describes even if SoftIntegration has been advised of the errors or omissions The Ch language environment is not designed or licensed for use in the on line control of aircraft air traffic or navigation or aircraft communications or for use in the design construction operation or maintenance of any nuclear facility Ch SoftIntegration and One Language for All are either registered trademarks or trademarks of SoftIn tegration Inc in the United States and or other countries
405. y pointing the mouse on Programs followed by Ch6 1 Finally select Ch e Keycaps the labeling that appears on the keys of a keyboard are enclosed in angle brackets The label of a keycap is presented in typewriter like typeface Example Press Enter iii Key combination is a series of keys to be pressed simultaneously unless otherwise indicated to perform a single function The label of the keycaps is presented in typewriter like typeface Example Press Ctrl Alt Enter Commands presented in lowercase boldface are for reference only and are not intended to be typed at that particular point in the discussion Example Use the install command to install In contrast commands presented in the typewriter like typeface are intended to be typed as part of an instruction Example Type install to install the software in the current directory Command Syntax lines consist of a command and all its possible parameters Commands are dis played in lowercase bold variable parameters those for which you substitute a value are displayed in lowercase italics constant parameters are displayed in lowercase bold The brackets indicate items that are optional Example Is aAbcCdfFgilLmnopgrRstux1 file Command lines consist of a command and may include one or more of the command s possible parameters Command lines are presented in the typewriter like typeface Example 1s home username Screen text is a
406. ype Ch_ArrayType Ch_FuncArgArrayType 313 Chapter A APIs for Embedding Ch lt embedch h gt Ch UserDefinedMemInfoByName Ch UserDefinedMemInfoByName Synopsis include lt ch h gt int Ch_UserDefinedMemInfoByName ChInterp_t interp ChUserDefinedTag t udtag const char memname ChMemlnfo t meminfo Purpose Obtain the information for a member of variable of the user defined structure class or union type based on the name of the member Return Value If the function is successul it return CH OK otherwise it returns CH ERROR Parameters interp A Ch interpreter udtag The tag for a user defined type a return value from the function Ch UserDefinedTag memname The name of the member meminfo A pointer to structure containing the information for a member of a user defined type Description This function Ch UserDefinedMemInfoByName can be used to obtain the information for a member of variable of the user defined structure class or union type based on its name of the member The function Ch UserDefinedMemlInfoByIndex can be used to obtain the informa tion for a member based on the index number of the member in the symbol table for its type If the input argument udtag is NULL or index is not a valid index of the user defined type the function returns CH ERRORand the member dt ype of the input argument meminfo is assigned with the value of CH UNDEFINETYPE This value can alos be used to test if the index isa

Download Pdf Manuals

image

Related Search

Related Contents

製品や設備などを 上手に撮るデジカメテクニック  Le Livre 010101 (1998  USER GUIDE - SUPERCONSOLE 1000®  HP ePrint Enterprise Setup and Install  STEAM STM-2 User Manual - STEAM Electronic Cigarettes  AEG S70170TK38 refrigerator  Manager User Manual  APECS User Manual  HP Compaq Pro XZ811UT  Extron electronic Scan Converter VSC 500 User's Manual  

Copyright © All rights reserved.
Failed to retrieve file