| | @@ -6,11 +6,11 @@ |
| 6 | 6 | ** This program is distributed in the hope that it will be useful, |
| 7 | 7 | ** but without any warranty; without even the implied warranty of |
| 8 | 8 | ** merchantability or fitness for a particular purpose. |
| 9 | 9 | ** |
| 10 | 10 | ******************************************************************************* |
| 11 | | -** This file contains code used to bridge the TH1 and Tcl scripting languages. |
| 11 | +** This file contains code used to bridge the Jim and Tcl scripting languages. |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | #include "config.h" |
| 15 | 15 | |
| 16 | 16 | #ifdef FOSSIL_ENABLE_TCL |
| | @@ -30,23 +30,23 @@ |
| 30 | 30 | #define USE_TCL_EVALOBJV 1 |
| 31 | 31 | #endif |
| 32 | 32 | |
| 33 | 33 | /* |
| 34 | 34 | ** These macros are designed to reduce the redundant code required to marshal |
| 35 | | -** arguments from TH1 to Tcl. |
| 35 | +** arguments from Jim to Tcl. |
| 36 | 36 | */ |
| 37 | 37 | #define USE_ARGV_TO_OBJV() \ |
| 38 | 38 | int objc; \ |
| 39 | 39 | Tcl_Obj **objv; \ |
| 40 | 40 | int i; |
| 41 | 41 | |
| 42 | | -#define COPY_ARGV_TO_OBJV() \ |
| 43 | | - objc = argc-1; \ |
| 44 | | - objv = (Tcl_Obj **)ckalloc((unsigned)(objc * sizeof(Tcl_Obj *))); \ |
| 45 | | - for(i=1; i<argc; i++){ \ |
| 46 | | - objv[i-1] = Tcl_NewStringObj(Jim_String(argv[i]), Jim_Length(argv[i])); \ |
| 47 | | - Tcl_IncrRefCount(objv[i-1]); \ |
| 42 | +#define COPY_ARGV_TO_OBJV() \ |
| 43 | + objc = argc-1; \ |
| 44 | + objv = (Tcl_Obj **)ckalloc((unsigned)(objc * sizeof(Tcl_Obj *))); \ |
| 45 | + for(i=1; i<argc; i++){ \ |
| 46 | + objv[i-1] = Tcl_NewStringObj(Jim_String(argv[i]), Jim_Length(argv[i])); \ |
| 47 | + Tcl_IncrRefCount(objv[i-1]); \ |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | #define FREE_ARGV_TO_OBJV() \ |
| 51 | 51 | for(i=1; i<argc; i++){ \ |
| 52 | 52 | Tcl_DecrRefCount(objv[i-1]); \ |
| | @@ -59,11 +59,11 @@ |
| 59 | 59 | */ |
| 60 | 60 | #define GET_CTX_TCL_INTERP(ctx) \ |
| 61 | 61 | ((struct TclContext *)(ctx))->interp |
| 62 | 62 | |
| 63 | 63 | /* |
| 64 | | -** Creates and initializes a Tcl interpreter for use with the specified TH1 |
| 64 | +** Creates and initializes a Tcl interpreter for use with the specified Jim |
| 65 | 65 | ** interpreter. Stores the created Tcl interpreter in the Tcl context supplied |
| 66 | 66 | ** by the caller. This must be declared here because quite a few functions in |
| 67 | 67 | ** this file need to use it before it can be defined. |
| 68 | 68 | */ |
| 69 | 69 | static int createTclInterp(Jim_Interp *interp, void *pContext); |
| | @@ -89,11 +89,11 @@ |
| 89 | 89 | } |
| 90 | 90 | return Tcl_GetStringFromObj(resultPtr, pN); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /* |
| 94 | | -** Tcl context information used by TH1. This structure definition has been |
| 94 | +** Tcl context information used by Jim. This structure definition has been |
| 95 | 95 | ** copied from and should be kept in sync with the one in "main.c". |
| 96 | 96 | */ |
| 97 | 97 | struct TclContext { |
| 98 | 98 | int argc; |
| 99 | 99 | char **argv; |
| | @@ -103,11 +103,14 @@ |
| 103 | 103 | /* |
| 104 | 104 | ** Syntax: |
| 105 | 105 | ** |
| 106 | 106 | ** tclEval arg ?arg ...? |
| 107 | 107 | */ |
| 108 | | -static int tclEval_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv |
| 108 | +static int tclEval_command( |
| 109 | + Jim_Interp *interp, |
| 110 | + int argc, |
| 111 | + Jim_Obj *const *argv |
| 109 | 112 | ){ |
| 110 | 113 | Tcl_Interp *tclInterp; |
| 111 | 114 | Tcl_Obj *objPtr; |
| 112 | 115 | int rc; |
| 113 | 116 | int nResult; |
| | @@ -150,11 +153,14 @@ |
| 150 | 153 | /* |
| 151 | 154 | ** Syntax: |
| 152 | 155 | ** |
| 153 | 156 | ** tclExpr arg ?arg ...? |
| 154 | 157 | */ |
| 155 | | -static int tclExpr_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv |
| 158 | +static int tclExpr_command( |
| 159 | + Jim_Interp *interp, |
| 160 | + int argc, |
| 161 | + Jim_Obj *const *argv |
| 156 | 162 | ){ |
| 157 | 163 | Tcl_Interp *tclInterp; |
| 158 | 164 | Tcl_Obj *objPtr; |
| 159 | 165 | Tcl_Obj *resultObjPtr; |
| 160 | 166 | int rc; |
| | @@ -203,11 +209,14 @@ |
| 203 | 209 | /* |
| 204 | 210 | ** Syntax: |
| 205 | 211 | ** |
| 206 | 212 | ** tclInvoke command ?arg ...? |
| 207 | 213 | */ |
| 208 | | -static int tclInvoke_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv |
| 214 | +static int tclInvoke_command( |
| 215 | + Jim_Interp *interp, |
| 216 | + int argc, |
| 217 | + Jim_Obj *const *argv |
| 209 | 218 | ){ |
| 210 | 219 | Tcl_Interp *tclInterp; |
| 211 | 220 | #ifndef USE_TCL_EVALOBJV |
| 212 | 221 | Tcl_Command command; |
| 213 | 222 | Tcl_CmdInfo cmdInfo; |
| | @@ -243,11 +252,12 @@ |
| 243 | 252 | Tcl_DecrRefCount(objPtr); |
| 244 | 253 | Tcl_Release((ClientData)tclInterp); |
| 245 | 254 | return JIM_ERR; |
| 246 | 255 | } |
| 247 | 256 | if( !cmdInfo.objProc ){ |
| 248 | | - Jim_SetResultFormatted(interp, "Cannot invoke command not found: %#s", argv[1]); |
| 257 | + Jim_SetResultFormatted(interp, "Cannot invoke command not found: %#s", |
| 258 | + argv[1]); |
| 249 | 259 | Tcl_DecrRefCount(objPtr); |
| 250 | 260 | Tcl_Release((ClientData)tclInterp); |
| 251 | 261 | return JIM_ERR; |
| 252 | 262 | } |
| 253 | 263 | Tcl_DecrRefCount(objPtr); |
| | @@ -267,108 +277,118 @@ |
| 267 | 277 | } |
| 268 | 278 | |
| 269 | 279 | /* |
| 270 | 280 | ** Syntax: |
| 271 | 281 | ** |
| 272 | | -** th1Eval arg |
| 282 | +** bridgeEval arg |
| 273 | 283 | */ |
| 274 | | -static int Th1EvalObjCmd( |
| 284 | +static int BridgeEvalObjCmd( |
| 275 | 285 | ClientData clientData, |
| 276 | 286 | Tcl_Interp *interp, |
| 277 | 287 | int objc, |
| 278 | 288 | Tcl_Obj *CONST objv[] |
| 279 | 289 | ){ |
| 280 | | - Jim_Interp *th1Interp; |
| 290 | + Jim_Interp *jimInterp; |
| 281 | 291 | int nArg; |
| 282 | 292 | const char *arg; |
| 283 | 293 | int rc; |
| 294 | + Jim_Obj *argObj; |
| 295 | + Jim_Obj *resultObj; |
| 284 | 296 | |
| 285 | 297 | if( objc!=2 ){ |
| 286 | 298 | Tcl_WrongNumArgs(interp, 1, objv, "arg"); |
| 287 | 299 | return TCL_ERROR; |
| 288 | 300 | } |
| 289 | | - th1Interp = (Jim_Interp *)clientData; |
| 290 | | - if( !th1Interp ){ |
| 291 | | - Tcl_AppendResult(interp, "invalid TH1 interpreter", NULL); |
| 301 | + jimInterp = (Jim_Interp *)clientData; |
| 302 | + if( !jimInterp ){ |
| 303 | + Tcl_AppendResult(interp, "invalid bridge interpreter", NULL); |
| 292 | 304 | return TCL_ERROR; |
| 293 | 305 | } |
| 294 | 306 | arg = Tcl_GetStringFromObj(objv[1], &nArg); |
| 295 | | - rc = Jim_Eval(th1Interp, arg); |
| 296 | | - arg = Jim_String(Jim_GetResult(th1Interp)); |
| 297 | | - Tcl_SetObjResult(interp, Tcl_NewStringObj(arg, -1)); |
| 307 | + argObj = Jim_NewStringObj(jimInterp, arg, nArg); |
| 308 | + Jim_IncrRefCount(argObj); |
| 309 | + rc = Jim_EvalObj(jimInterp, argObj); |
| 310 | + Jim_DecrRefCount(jimInterp, argObj); |
| 311 | + resultObj = Jim_GetResult(jimInterp); |
| 312 | + arg = Jim_GetString(resultObj, &nArg); |
| 313 | + Tcl_SetObjResult(interp, Tcl_NewStringObj(arg, nArg)); |
| 298 | 314 | return rc; |
| 299 | 315 | } |
| 300 | 316 | |
| 301 | 317 | /* |
| 302 | 318 | ** Syntax: |
| 303 | 319 | ** |
| 304 | | -** th1Expr arg |
| 320 | +** bridgeExpr arg |
| 305 | 321 | */ |
| 306 | | -static int Th1ExprObjCmd( |
| 322 | +static int BridgeExprObjCmd( |
| 307 | 323 | ClientData clientData, |
| 308 | 324 | Tcl_Interp *interp, |
| 309 | 325 | int objc, |
| 310 | 326 | Tcl_Obj *CONST objv[] |
| 311 | 327 | ){ |
| 312 | | - Jim_Interp *th1Interp; |
| 328 | + Jim_Interp *jimInterp; |
| 313 | 329 | int nArg; |
| 314 | 330 | const char *arg; |
| 315 | 331 | int rc; |
| 316 | | - Jim_Obj *exprResultObj; |
| 332 | + Jim_Obj *argObj; |
| 333 | + Jim_Obj *resultObj; |
| 317 | 334 | |
| 318 | 335 | if( objc!=2 ){ |
| 319 | 336 | Tcl_WrongNumArgs(interp, 1, objv, "arg"); |
| 320 | 337 | return TCL_ERROR; |
| 321 | 338 | } |
| 322 | | - th1Interp = (Jim_Interp *)clientData; |
| 323 | | - if( !th1Interp ){ |
| 324 | | - Tcl_AppendResult(interp, "invalid TH1 interpreter", NULL); |
| 339 | + jimInterp = (Jim_Interp *)clientData; |
| 340 | + if( !jimInterp ){ |
| 341 | + Tcl_AppendResult(interp, "invalid bridge interpreter", NULL); |
| 325 | 342 | return TCL_ERROR; |
| 326 | 343 | } |
| 327 | | - |
| 328 | 344 | arg = Tcl_GetStringFromObj(objv[1], &nArg); |
| 329 | | - rc = Jim_EvalExpression(th1Interp, Jim_NewStringObj(th1Interp, arg, -1), &exprResultObj); |
| 345 | + argObj = Jim_NewStringObj(jimInterp, arg, nArg); |
| 346 | + Jim_IncrRefCount(argObj); |
| 347 | + rc = Jim_EvalExpression(jimInterp, argObj, &resultObj); |
| 348 | + Jim_DecrRefCount(jimInterp, argObj); |
| 330 | 349 | if (rc == JIM_OK) { |
| 331 | | - arg = Jim_String(exprResultObj); |
| 332 | | - Tcl_SetObjResult(interp, Tcl_NewStringObj(arg, -1)); |
| 350 | + arg = Jim_GetString(resultObj, &nArg); |
| 351 | + Tcl_SetObjResult(interp, Tcl_NewStringObj(arg, nArg)); |
| 333 | 352 | } |
| 334 | | - |
| 335 | 353 | return rc; |
| 336 | 354 | } |
| 337 | 355 | |
| 338 | 356 | /* |
| 339 | 357 | ** Array of Tcl integration commands. Used when adding or removing the Tcl |
| 340 | | -** integration commands from TH1. |
| 358 | +** integration commands from Jim. |
| 341 | 359 | */ |
| 342 | 360 | static struct _Command { |
| 343 | 361 | const char *zName; |
| 344 | 362 | Jim_CmdProc xProc; |
| 363 | + void *pContext; |
| 345 | 364 | } aCommand[] = { |
| 346 | | - {"tclEval", tclEval_command }, |
| 347 | | - {"tclExpr", tclExpr_command }, |
| 348 | | - {"tclInvoke", tclInvoke_command}, |
| 365 | + {"tclEval", tclEval_command, 0}, |
| 366 | + {"tclExpr", tclExpr_command, 0}, |
| 367 | + {"tclInvoke", tclInvoke_command, 0}, |
| 368 | + {0, 0, 0} |
| 349 | 369 | }; |
| 350 | 370 | |
| 351 | 371 | /* |
| 352 | 372 | ** Called if the Tcl interpreter is deleted. Removes the Tcl integration |
| 353 | | -** commands from the TH1 interpreter. |
| 373 | +** commands from the Jim interpreter. |
| 354 | 374 | */ |
| 355 | | -static void Th1DeleteProc( |
| 375 | +static void BridgeDeleteProc( |
| 356 | 376 | ClientData clientData, |
| 357 | 377 | Tcl_Interp *interp |
| 358 | 378 | ){ |
| 359 | 379 | int i; |
| 360 | | - Jim_Interp *th1Interp = (Jim_Interp *)clientData; |
| 361 | | - if( !th1Interp ) return; |
| 380 | + Jim_Interp *jimInterp = (Jim_Interp *)clientData; |
| 381 | + if( !jimInterp ) return; |
| 362 | 382 | /* Remove the Tcl integration commands. */ |
| 363 | 383 | for(i=0; i<(sizeof(aCommand)/sizeof(aCommand[0])); i++){ |
| 364 | | - Jim_DeleteCommand(th1Interp, aCommand[i].zName); |
| 384 | + Jim_DeleteCommand(jimInterp, aCommand[i].zName); |
| 365 | 385 | } |
| 366 | 386 | } |
| 367 | 387 | |
| 368 | 388 | /* |
| 369 | | -** Creates and initializes a Tcl interpreter for use with the specified TH1 |
| 389 | +** Creates and initializes a Tcl interpreter for use with the specified Jim |
| 370 | 390 | ** interpreter. Stores the created Tcl interpreter in the Tcl context supplied |
| 371 | 391 | ** by the caller. |
| 372 | 392 | */ |
| 373 | 393 | static int createTclInterp( |
| 374 | 394 | Jim_Interp *interp, |
| | @@ -391,35 +411,38 @@ |
| 391 | 411 | if( !tclInterp || Tcl_InterpDeleted(tclInterp) ){ |
| 392 | 412 | Jim_SetResultString(interp, "Could not create Tcl interpreter", -1); |
| 393 | 413 | return JIM_ERR; |
| 394 | 414 | } |
| 395 | 415 | if( Tcl_Init(tclInterp)!=TCL_OK ){ |
| 396 | | - Jim_SetResultFormatted(interp, "Tcl initialization error: %s", Tcl_GetStringResult(tclInterp)); |
| 416 | + Jim_SetResultFormatted(interp, "Tcl initialization error: %s", |
| 417 | + Tcl_GetStringResult(tclInterp)); |
| 397 | 418 | Tcl_DeleteInterp(tclInterp); |
| 398 | 419 | tclContext->interp = tclInterp = 0; |
| 399 | 420 | return JIM_ERR; |
| 400 | 421 | } |
| 401 | | - /* Add the TH1 integration commands to Tcl. */ |
| 402 | | - Tcl_CallWhenDeleted(tclInterp, Th1DeleteProc, interp); |
| 403 | | - Tcl_CreateObjCommand(tclInterp, "th1Eval", Th1EvalObjCmd, interp, NULL); |
| 404 | | - Tcl_CreateObjCommand(tclInterp, "th1Expr", Th1ExprObjCmd, interp, NULL); |
| 422 | + /* Add the Jim integration commands to Tcl. */ |
| 423 | + Tcl_CallWhenDeleted(tclInterp, BridgeDeleteProc, interp); |
| 424 | + Tcl_CreateObjCommand(tclInterp, "bridgeEval", BridgeEvalObjCmd, interp, NULL); |
| 425 | + Tcl_CreateObjCommand(tclInterp, "bridgeExpr", BridgeExprObjCmd, interp, NULL); |
| 405 | 426 | return JIM_OK; |
| 406 | 427 | } |
| 407 | 428 | |
| 408 | 429 | /* |
| 409 | 430 | ** Register the Tcl language commands with interpreter interp. |
| 410 | 431 | ** Usually this is called soon after interpreter creation. |
| 411 | 432 | */ |
| 412 | | -int th_register_tcl( |
| 433 | +int register_tcl( |
| 413 | 434 | Jim_Interp *interp, |
| 414 | 435 | void *pContext |
| 415 | 436 | ){ |
| 416 | 437 | int i; |
| 417 | | - /* Add the Tcl integration commands to TH1. */ |
| 438 | + /* Add the Tcl integration commands to Jim. */ |
| 418 | 439 | for(i=0; i<(sizeof(aCommand)/sizeof(aCommand[0])); i++){ |
| 440 | + void *ctx = aCommand[i].pContext; |
| 419 | 441 | /* Use Tcl interpreter for context? */ |
| 420 | | - Jim_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc, pContext, NULL); |
| 442 | + if( !ctx ) ctx = pContext; |
| 443 | + Jim_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc, ctx, NULL); |
| 421 | 444 | } |
| 422 | 445 | return JIM_OK; |
| 423 | 446 | } |
| 424 | 447 | |
| 425 | 448 | #endif /* FOSSIL_ENABLE_TCL */ |
| 426 | 449 | |