| 1 | #include <tcl.h> |
|---|
| 2 | #include "toltcl.h" |
|---|
| 3 | |
|---|
| 4 | #if defined(WIN32) |
|---|
| 5 | #include "windows.h" |
|---|
| 6 | #else |
|---|
| 7 | #include "tol/tol_init.h" |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | static int g_argc = 0; |
|---|
| 11 | static char **g_argv = NULL; |
|---|
| 12 | |
|---|
| 13 | static |
|---|
| 14 | void SetupArgv( Tcl_Interp *interp ) |
|---|
| 15 | { |
|---|
| 16 | Tcl_Obj *argvPtr = Tcl_NewListObj(0, NULL); |
|---|
| 17 | int i; |
|---|
| 18 | for( i = 1; i < g_argc; i++ ) |
|---|
| 19 | { |
|---|
| 20 | Tcl_DString ds; |
|---|
| 21 | Tcl_ExternalToUtfDString(NULL, g_argv[i], -1, &ds); |
|---|
| 22 | Tcl_ListObjAppendElement(NULL, argvPtr, |
|---|
| 23 | Tcl_NewStringObj(Tcl_DStringValue(&ds), |
|---|
| 24 | Tcl_DStringLength(&ds))); |
|---|
| 25 | Tcl_DStringFree(&ds); |
|---|
| 26 | } |
|---|
| 27 | Tcl_SetVar2Ex(interp, "argv", NULL, argvPtr, TCL_GLOBAL_ONLY); |
|---|
| 28 | Tcl_SetVar2Ex(interp, "argc", NULL, Tcl_NewIntObj(g_argc-1), TCL_GLOBAL_ONLY); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | #if defined(WIN32) |
|---|
| 32 | void SetupDllSearchPath( const char *argv0 ) |
|---|
| 33 | { |
|---|
| 34 | int n; |
|---|
| 35 | //const char **strArr; |
|---|
| 36 | const char **ptr; |
|---|
| 37 | Tcl_DString dstr; |
|---|
| 38 | |
|---|
| 39 | //ptr = &strArr; |
|---|
| 40 | Tcl_SplitPath( argv0, &n, &ptr ); |
|---|
| 41 | if( n >= 2 ) |
|---|
| 42 | { |
|---|
| 43 | Tcl_DStringInit( &dstr ); |
|---|
| 44 | Tcl_JoinPath( n - 1, ptr, &dstr ); |
|---|
| 45 | SetDllDirectory( Tcl_DStringValue( &dstr ) ); |
|---|
| 46 | Tcl_DStringFree( &dstr ); |
|---|
| 47 | } |
|---|
| 48 | Tcl_Free( (char*)ptr ); |
|---|
| 49 | } |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | int AppInit(Tcl_Interp *interp) |
|---|
| 53 | { |
|---|
| 54 | if( Tcl_Init(interp) == TCL_ERROR ) |
|---|
| 55 | { |
|---|
| 56 | return TCL_ERROR; |
|---|
| 57 | } |
|---|
| 58 | SetupArgv( interp ); |
|---|
| 59 | const char *argv0 = Tcl_GetNameOfExecutable(); |
|---|
| 60 | #if defined(WIN32) |
|---|
| 61 | SetupDllSearchPath( argv0 ); |
|---|
| 62 | Tcl_VarEval( interp, "lappend", " auto_path", " [file join", |
|---|
| 63 | " [file dir [file dir {", argv0, |
|---|
| 64 | "} ] ] lib ]", NULL ); |
|---|
| 65 | #else |
|---|
| 66 | Tcl_VarEval( interp, "lappend", " auto_path", " [file join", |
|---|
| 67 | " [file dir [file dir [expr {[file type {", argv0, "}] eq {link} ? [file readlink {", argv0, "}] : {", argv0 ,"}} ] ] ] lib ]", NULL ); |
|---|
| 68 | #endif |
|---|
| 69 | if ( !Tcl_PkgRequire( interp, "TolshApp", TOLTCL_VERSION, 1) ) |
|---|
| 70 | { |
|---|
| 71 | printf( "%s\n", Tcl_GetStringResult( interp ) ); |
|---|
| 72 | Tcl_Exit( -1 ); |
|---|
| 73 | } |
|---|
| 74 | return TCL_OK; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | int main(int argc, char *argv[]) |
|---|
| 78 | { |
|---|
| 79 | g_argc = argc; |
|---|
| 80 | g_argv = argv; |
|---|
| 81 | #if !defined(WIN32) |
|---|
| 82 | const char * version = TOLVersion(); |
|---|
| 83 | #endif |
|---|
| 84 | Tcl_Main( 1, argv, AppInit ); |
|---|
| 85 | return 0; |
|---|
| 86 | } |
|---|