1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
|
/*
* ------------------------------------------------------------------------
* PACKAGE: [incr Tcl]
* DESCRIPTION: Object-Oriented Extensions to Tcl
*
* [incr Tcl] provides object-oriented extensions to Tcl, much as
* C++ provides object-oriented extensions to C. It provides a means
* of encapsulating related procedures together with their shared data
* in a local namespace that is hidden from the outside world. It
* promotes code re-use through inheritance. More than anything else,
* it encourages better organization of Tcl applications through the
* object-oriented paradigm, leading to code that is easier to
* understand and maintain.
*
* This file defines information that tracks classes and objects
* at a global level for a given interpreter.
*
* ========================================================================
* AUTHOR: Michael J. McLennan
* Bell Labs Innovations for Lucent Technologies
* mmclennan@lucent.com
* http://www.tcltk.com/itcl
*
* RCS: $Id: itcl_cmds.c,v 1.13 2001/05/22 01:50:21 davygrvy Exp $
* ========================================================================
* Copyright (c) 1993-1998 Lucent Technologies, Inc.
* ------------------------------------------------------------------------
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "itclInt.h"
/*
* FORWARD DECLARATIONS
*/
static void ItclDelObjectInfo _ANSI_ARGS_((char* cdata));
static int Initialize _ANSI_ARGS_((Tcl_Interp *interp));
static int ItclHandleStubCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
static void ItclDeleteStub _ANSI_ARGS_((ClientData cdata));
/*
* The following string is the startup script executed in new
* interpreters. It locates the Tcl code in the [incr Tcl] library
* directory and loads it in.
*/
static char initScript[] = "\n\
namespace eval ::itcl {\n\
proc _find_init {} {\n\
global env tcl_library\n\
variable library\n\
variable version\n\
rename _find_init {}\n\
if {[info exists library]} {\n\
lappend dirs $library\n\
} else {\n\
if {[catch {uplevel #0 source -rsrc itcl}] == 0} {\n\
return\n\
}\n\
set dirs {}\n\
if {[info exists env(ITCL_LIBRARY)]} {\n\
lappend dirs $env(ITCL_LIBRARY)\n\
}\n\
lappend dirs [file join [file dirname $tcl_library] itcl$version]\n\
set bindir [file dirname [info nameofexecutable]]\n\
lappend dirs [file join $bindir .. lib itcl$version]\n\
lappend dirs [file join $bindir .. library]\n\
lappend dirs [file join $bindir .. .. library]\n\
lappend dirs [file join $bindir .. .. itcl library]\n\
lappend dirs [file join $bindir .. .. .. itcl library]\n\
}\n\
foreach i $dirs {\n\
set library $i\n\
set itclfile [file join $i itcl.tcl]\n\
if {![catch {uplevel #0 [list source $itclfile]} msg]} {\n\
return\n\
}\n\
}\n\
set msg \"Can't find a usable itcl.tcl in the following directories:\n\"\n\
append msg \" $dirs\n\"\n\
append msg \"This probably means that Itcl/Tcl weren't installed properly.\n\"\n\
append msg \"If you know where the Itcl library directory was installed,\n\"\n\
append msg \"you can set the environment variable ITCL_LIBRARY to point\n\"\n\
append msg \"to the library directory.\n\"\n\
error $msg\n\
}\n\
_find_init\n\
}";
/*
* The following script is used to initialize Itcl in a safe interpreter.
*/
static char safeInitScript[] =
"proc ::itcl::local {class name args} {\n\
set ptr [uplevel [list $class $name] $args]\n\
uplevel [list set itcl-local-$ptr $ptr]\n\
set cmd [uplevel namespace which -command $ptr]\n\
uplevel [list trace variable itcl-local-$ptr u \"::itcl::delete object $cmd; list\"]\n\
return $ptr\n\
}";
extern ItclStubs itclStubs;
int itclCompatFlags = -1;
/*
* ------------------------------------------------------------------------
* Initialize()
*
* Invoked whenever a new interpeter is created to install the
* [incr Tcl] package. Usually invoked within Tcl_AppInit() at
* the start of execution.
*
* Creates the "::itcl" namespace and installs access commands for
* creating classes and querying info.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error
* message in the interpreter) if anything goes wrong.
* ------------------------------------------------------------------------
*/
static int
Initialize(interp)
Tcl_Interp *interp; /* interpreter to be updated */
{
Tcl_CmdInfo cmdInfo;
Tcl_Namespace *itclNs;
ItclObjectInfo *info;
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
};
/*
* See if [incr Tcl] is already installed.
*/
if (Tcl_GetCommandInfo(interp, "::itcl::class", &cmdInfo)) {
Tcl_SetResult(interp, "already installed: [incr Tcl]", TCL_STATIC);
return TCL_ERROR;
}
/*
* Set the compatability options. Stubs allows us to load into many
* version of the Tcl core. Some problems have crept-in, and we need
* to adapt dynamically regarding use of some internal structures that
* have changed since 8.1.0
*
* TODO: make a TIP for exporting a Tcl_CommandIsDeleted function in the core.
*/
if (itclCompatFlags == -1) {
int maj, min, ptch, type;
itclCompatFlags = 0;
Tcl_GetVersion(&maj, &min, &ptch, &type);
if ((maj == 8) && (min >= 4)) {
itclCompatFlags = ITCL_COMPAT_USECMDFLAGS;
}
}
/*
* Initialize the ensemble package first, since we need this
* for other parts of [incr Tcl].
*/
if (Itcl_EnsembleInit(interp) != TCL_OK) {
return TCL_ERROR;
}
/*
* Create the top-level data structure for tracking objects.
* Store this as "associated data" for easy access, but link
* it to the itcl namespace for ownership.
*/
info = (ItclObjectInfo*)ckalloc(sizeof(ItclObjectInfo));
info->interp = interp;
Tcl_InitHashTable(&info->objects, TCL_ONE_WORD_KEYS);
Itcl_InitStack(&info->transparentFrames);
Tcl_InitHashTable(&info->contextFrames, TCL_ONE_WORD_KEYS);
info->protection = ITCL_DEFAULT_PROTECT;
Itcl_InitStack(&info->cdefnStack);
Tcl_SetAssocData(interp, ITCL_INTERP_DATA,
(Tcl_InterpDeleteProc*)NULL, (ClientData)info);
/*
* Install commands into the "::itcl" namespace.
*/
Tcl_CreateObjCommand(interp, "::itcl::class", Itcl_ClassCmd,
(ClientData)info, Itcl_ReleaseData);
Itcl_PreserveData((ClientData)info);
Tcl_CreateObjCommand(interp, "::itcl::body", Itcl_BodyCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateObjCommand(interp, "::itcl::configbody", Itcl_ConfigBodyCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Itcl_EventuallyFree((ClientData)info, ItclDelObjectInfo);
/*
* Create the "itcl::find" command for high-level queries.
*/
if (Itcl_CreateEnsemble(interp, "::itcl::find") != TCL_OK) {
return TCL_ERROR;
}
if (Itcl_AddEnsemblePart(interp, "::itcl::find",
"classes", "?pattern?",
Itcl_FindClassesCmd,
(ClientData)info, Itcl_ReleaseData) != TCL_OK) {
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)info);
if (Itcl_AddEnsemblePart(interp, "::itcl::find",
"objects", "?-class className? ?-isa className? ?pattern?",
Itcl_FindObjectsCmd,
(ClientData)info, Itcl_ReleaseData) != TCL_OK) {
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)info);
/*
* Create the "itcl::delete" command to delete objects
* and classes.
*/
if (Itcl_CreateEnsemble(interp, "::itcl::delete") != TCL_OK) {
return TCL_ERROR;
}
if (Itcl_AddEnsemblePart(interp, "::itcl::delete",
"class", "name ?name...?",
Itcl_DelClassCmd,
(ClientData)info, Itcl_ReleaseData) != TCL_OK) {
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)info);
if (Itcl_AddEnsemblePart(interp, "::itcl::delete",
"object", "name ?name...?",
Itcl_DelObjectCmd,
(ClientData)info, Itcl_ReleaseData) != TCL_OK) {
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)info);
/*
* Add "code" and "scope" commands for handling scoped values.
*/
Tcl_CreateObjCommand(interp, "::itcl::code", Itcl_CodeCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateObjCommand(interp, "::itcl::scope", Itcl_ScopeCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
/*
* Add commands for handling import stubs at the Tcl level.
*/
if (Itcl_CreateEnsemble(interp, "::itcl::import::stub") != TCL_OK) {
return TCL_ERROR;
}
if (Itcl_AddEnsemblePart(interp, "::itcl::import::stub",
"create", "name", Itcl_StubCreateCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL) != TCL_OK) {
return TCL_ERROR;
}
if (Itcl_AddEnsemblePart(interp, "::itcl::import::stub",
"exists", "name", Itcl_StubExistsCmd,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL) != TCL_OK) {
return TCL_ERROR;
}
/*
* Install a variable resolution procedure to handle scoped
* values everywhere within the interpreter.
*/
Tcl_AddInterpResolvers(interp, "itcl", (Tcl_ResolveCmdProc*)NULL,
Itcl_ScopedVarResolver, (Tcl_ResolveCompiledVarProc*)NULL);
/*
* Install the "itcl::parser" namespace used to parse the
* class definitions.
*/
if (Itcl_ParseInit(interp, info) != TCL_OK) {
return TCL_ERROR;
}
/*
* Create "itcl::builtin" namespace for commands that
* are automatically built into class definitions.
*/
if (Itcl_BiInit(interp) != TCL_OK) {
return TCL_ERROR;
}
/*
* Install stuff needed for backward compatibility with previous
* version of [incr Tcl].
*/
if (Itcl_OldInit(interp, info) != TCL_OK) {
return TCL_ERROR;
}
/*
* Export all commands in the "itcl" namespace so that they
* can be imported with something like "namespace import itcl::*"
*/
itclNs = Tcl_FindNamespace(interp, "::itcl", (Tcl_Namespace*)NULL,
TCL_LEAVE_ERR_MSG);
if (!itclNs ||
Tcl_Export(interp, itclNs, "*", /* resetListFirst */ 1) != TCL_OK) {
return TCL_ERROR;
}
/*
* Set up the variables containing version info.
*/
Tcl_SetVar(interp, "::itcl::patchLevel", ITCL_PATCH_LEVEL,
TCL_NAMESPACE_ONLY);
Tcl_SetVar(interp, "::itcl::version", ITCL_VERSION,
TCL_NAMESPACE_ONLY);
/*
* Package is now loaded.
*/
if (Tcl_PkgProvideEx(interp, "Itcl", ITCL_VERSION,
(ClientData) &itclStubs) != TCL_OK) {
return TCL_ERROR;
}
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_Init()
*
* Invoked whenever a new INTERPRETER is created to install the
* [incr Tcl] package. Usually invoked within Tcl_AppInit() at
* the start of execution.
*
* Creates the "::itcl" namespace and installs access commands for
* creating classes and querying info.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error
* message in the interpreter) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_Init(interp)
Tcl_Interp *interp; /* interpreter to be updated */
{
if (Initialize(interp) != TCL_OK) {
return TCL_ERROR;
}
return Tcl_Eval(interp, initScript);
}
/*
* ------------------------------------------------------------------------
* Itcl_SafeInit()
*
* Invoked whenever a new SAFE INTERPRETER is created to install
* the [incr Tcl] package.
*
* Creates the "::itcl" namespace and installs access commands for
* creating classes and querying info.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error
* message in the interpreter) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_SafeInit(interp)
Tcl_Interp *interp; /* interpreter to be updated */
{
if (Initialize(interp) != TCL_OK) {
return TCL_ERROR;
}
return Tcl_Eval(interp, safeInitScript);
}
/*
* ------------------------------------------------------------------------
* ItclDelObjectInfo()
*
* Invoked when the management info for [incr Tcl] is no longer being
* used in an interpreter. This will only occur when all class
* manipulation commands are removed from the interpreter.
* ------------------------------------------------------------------------
*/
static void
ItclDelObjectInfo(cdata)
char* cdata; /* client data for class command */
{
ItclObjectInfo *info = (ItclObjectInfo*)cdata;
ItclObject *contextObj;
Tcl_HashSearch place;
Tcl_HashEntry *entry;
/*
* Destroy all known objects by deleting their access
* commands.
*/
entry = Tcl_FirstHashEntry(&info->objects, &place);
while (entry) {
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
Tcl_DeleteCommandFromToken(info->interp, contextObj->accessCmd);
/*
* Fix 227804: Whenever an object to delete was found we
* have to reset the search to the beginning as the
* current entry in the search was deleted and accessing it
* is therefore not allowed anymore.
*/
entry = Tcl_FirstHashEntry(&info->objects, &place);
/*entry = Tcl_NextHashEntry(&place);*/
}
Tcl_DeleteHashTable(&info->objects);
/*
* Discard all known object contexts.
*/
entry = Tcl_FirstHashEntry(&info->contextFrames, &place);
while (entry) {
Itcl_ReleaseData( Tcl_GetHashValue(entry) );
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(&info->contextFrames);
Itcl_DeleteStack(&info->transparentFrames);
Itcl_DeleteStack(&info->cdefnStack);
ckfree((char*)info);
}
/*
* ------------------------------------------------------------------------
* Itcl_FindClassesCmd()
*
* Invoked by Tcl whenever the user issues an "itcl::find classes"
* command to query the list of known classes. Handles the following
* syntax:
*
* find classes ?<pattern>?
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_FindClassesCmd(clientData, interp, objc, objv)
ClientData clientData; /* class/object info */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
Tcl_Namespace *activeNs = Tcl_GetCurrentNamespace(interp);
Tcl_Namespace *globalNs = Tcl_GetGlobalNamespace(interp);
int forceFullNames = 0;
char *pattern;
CONST char *name;
int newEntry, handledActiveNs;
Tcl_HashTable unique;
Tcl_HashEntry *entry;
Tcl_HashSearch place;
Itcl_Stack search;
Tcl_Command cmd, originalCmd;
Namespace *nsPtr;
Tcl_Obj *listPtr, *objPtr;
if (objc > 2) {
Tcl_WrongNumArgs(interp, 1, objv, "?pattern?");
return TCL_ERROR;
}
if (objc == 2) {
pattern = Tcl_GetStringFromObj(objv[1], (int*)NULL);
forceFullNames = (strstr(pattern, "::") != NULL);
} else {
pattern = NULL;
}
/*
* Search through all commands in the current namespace first,
* in the global namespace next, then in all child namespaces
* in this interpreter. If we find any commands that
* represent classes, report them.
*/
listPtr = Tcl_NewListObj(0, (Tcl_Obj* CONST*)NULL);
Itcl_InitStack(&search);
Itcl_PushStack((ClientData)globalNs, &search);
Itcl_PushStack((ClientData)activeNs, &search); /* last in, first out! */
Tcl_InitHashTable(&unique, TCL_ONE_WORD_KEYS);
handledActiveNs = 0;
while (Itcl_GetStackSize(&search) > 0) {
nsPtr = (Namespace*)Itcl_PopStack(&search);
if (nsPtr == (Namespace*)activeNs && handledActiveNs) {
continue;
}
entry = Tcl_FirstHashEntry(&nsPtr->cmdTable, &place);
while (entry) {
cmd = (Tcl_Command)Tcl_GetHashValue(entry);
if (Itcl_IsClass(cmd)) {
originalCmd = TclGetOriginalCommand(cmd);
/*
* Report full names if:
* - the pattern has namespace qualifiers
* - the class namespace is not in the current namespace
* - the class's object creation command is imported from
* another namespace.
*
* Otherwise, report short names.
*/
if (forceFullNames || nsPtr != (Namespace*)activeNs ||
originalCmd != NULL) {
objPtr = Tcl_NewStringObj((char*)NULL, 0);
Tcl_GetCommandFullName(interp, cmd, objPtr);
name = Tcl_GetStringFromObj(objPtr, (int*)NULL);
} else {
name = Tcl_GetCommandName(interp, cmd);
objPtr = Tcl_NewStringObj(name, -1);
}
if (originalCmd) {
cmd = originalCmd;
}
Tcl_CreateHashEntry(&unique, (char*)cmd, &newEntry);
if (newEntry && (!pattern || Tcl_StringMatch(name, pattern))) {
Tcl_ListObjAppendElement((Tcl_Interp*)NULL,
listPtr, objPtr);
}
}
entry = Tcl_NextHashEntry(&place);
}
handledActiveNs = 1; /* don't process the active namespace twice */
/*
* Push any child namespaces onto the stack and continue
* the search in those namespaces.
*/
entry = Tcl_FirstHashEntry(&nsPtr->childTable, &place);
while (entry != NULL) {
Itcl_PushStack(Tcl_GetHashValue(entry), &search);
entry = Tcl_NextHashEntry(&place);
}
}
Tcl_DeleteHashTable(&unique);
Itcl_DeleteStack(&search);
Tcl_SetObjResult(interp, listPtr);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_FindObjectsCmd()
*
* Invoked by Tcl whenever the user issues an "itcl::find objects"
* command to query the list of known objects. Handles the following
* syntax:
*
* find objects ?-class <className>? ?-isa <className>? ?<pattern>?
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
int
Itcl_FindObjectsCmd(clientData, interp, objc, objv)
ClientData clientData; /* class/object info */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
Tcl_Namespace *activeNs = Tcl_GetCurrentNamespace(interp);
Tcl_Namespace *globalNs = Tcl_GetGlobalNamespace(interp);
int forceFullNames = 0;
char *pattern = NULL;
ItclClass *classDefn = NULL;
ItclClass *isaDefn = NULL;
char *name, *token;
CONST char *cmdName;
int pos, newEntry, match, handledActiveNs;
ItclObject *contextObj;
Tcl_HashTable unique;
Tcl_HashEntry *entry;
Tcl_HashSearch place;
Itcl_Stack search;
Tcl_Command cmd, originalCmd;
Namespace *nsPtr;
Command *cmdPtr;
Tcl_Obj *listPtr, *objPtr;
/*
* Parse arguments:
* objects ?-class <className>? ?-isa <className>? ?<pattern>?
*/
pos = 0;
while (++pos < objc) {
token = Tcl_GetStringFromObj(objv[pos], (int*)NULL);
if (*token != '-') {
if (!pattern) {
pattern = token;
forceFullNames = (strstr(pattern, "::") != NULL);
} else {
break;
}
}
else if ((pos+1 < objc) && (strcmp(token,"-class") == 0)) {
name = Tcl_GetStringFromObj(objv[pos+1], (int*)NULL);
classDefn = Itcl_FindClass(interp, name, /* autoload */ 1);
if (classDefn == NULL) {
return TCL_ERROR;
}
pos++;
}
else if ((pos+1 < objc) && (strcmp(token,"-isa") == 0)) {
name = Tcl_GetStringFromObj(objv[pos+1], (int*)NULL);
isaDefn = Itcl_FindClass(interp, name, /* autoload */ 1);
if (isaDefn == NULL) {
return TCL_ERROR;
}
pos++;
}
/*
* Last token? Take it as the pattern, even if it starts
* with a "-". This allows us to match object names that
* start with "-".
*/
else if (pos == objc-1 && !pattern) {
pattern = token;
forceFullNames = (strstr(pattern, "::") != NULL);
}
else {
break;
}
}
if (pos < objc) {
Tcl_WrongNumArgs(interp, 1, objv,
"?-class className? ?-isa className? ?pattern?");
return TCL_ERROR;
}
/*
* Search through all commands in the current namespace first,
* in the global namespace next, then in all child namespaces
* in this interpreter. If we find any commands that
* represent objects, report them.
*/
listPtr = Tcl_NewListObj(0, (Tcl_Obj* CONST*)NULL);
Itcl_InitStack(&search);
Itcl_PushStack((ClientData)globalNs, &search);
Itcl_PushStack((ClientData)activeNs, &search); /* last in, first out! */
Tcl_InitHashTable(&unique, TCL_ONE_WORD_KEYS);
handledActiveNs = 0;
while (Itcl_GetStackSize(&search) > 0) {
nsPtr = (Namespace*)Itcl_PopStack(&search);
if (nsPtr == (Namespace*)activeNs && handledActiveNs) {
continue;
}
entry = Tcl_FirstHashEntry(&nsPtr->cmdTable, &place);
while (entry) {
cmd = (Tcl_Command)Tcl_GetHashValue(entry);
if (Itcl_IsObject(cmd)) {
originalCmd = TclGetOriginalCommand(cmd);
if (originalCmd) {
cmd = originalCmd;
}
cmdPtr = (Command*)cmd;
contextObj = (ItclObject*)cmdPtr->objClientData;
/*
* Report full names if:
* - the pattern has namespace qualifiers
* - the class namespace is not in the current namespace
* - the class's object creation command is imported from
* another namespace.
*
* Otherwise, report short names.
*/
if (forceFullNames || nsPtr != (Namespace*)activeNs ||
originalCmd != NULL) {
objPtr = Tcl_NewStringObj((char*)NULL, 0);
Tcl_GetCommandFullName(interp, cmd, objPtr);
name = Tcl_GetStringFromObj(objPtr, (int*)NULL);
} else {
cmdName = Tcl_GetCommandName(interp, cmd);
objPtr = Tcl_NewStringObj(cmdName, -1);
}
Tcl_CreateHashEntry(&unique, (char*)cmd, &newEntry);
match = 0;
if (newEntry && (!pattern || Tcl_StringMatch(name, pattern))) {
if (!classDefn || (contextObj->classDefn == classDefn)) {
if (!isaDefn) {
match = 1;
} else {
entry = Tcl_FindHashEntry(
&contextObj->classDefn->heritage,
(char*)isaDefn);
if (entry) {
match = 1;
}
}
}
}
if (match) {
Tcl_ListObjAppendElement((Tcl_Interp*)NULL,
listPtr, objPtr);
} else {
Tcl_IncrRefCount(objPtr); /* throw away the name */
Tcl_DecrRefCount(objPtr);
}
}
entry = Tcl_NextHashEntry(&place);
}
handledActiveNs = 1; /* don't process the active namespace twice */
/*
* Push any child namespaces onto the stack and continue
* the search in those namespaces.
*/
entry = Tcl_FirstHashEntry(&nsPtr->childTable, &place);
while (entry != NULL) {
Itcl_PushStack(Tcl_GetHashValue(entry), &search);
entry = Tcl_NextHashEntry(&place);
}
}
Tcl_DeleteHashTable(&unique);
Itcl_DeleteStack(&search);
Tcl_SetObjResult(interp, listPtr);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_ProtectionCmd()
*
* Invoked by Tcl whenever the user issues a protection setting
* command like "public" or "private". Creates commands and
* variables, and assigns a protection level to them. Protection
* levels are defined as follows:
*
* public => accessible from any namespace
* protected => accessible from selected namespaces
* private => accessible only in the namespace where it was defined
*
* Handles the following syntax:
*
* public <command> ?<arg> <arg>...?
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
int
Itcl_ProtectionCmd(clientData, interp, objc, objv)
ClientData clientData; /* protection level (public/protected/private) */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int pLevel = (int)clientData;
int result;
int oldLevel;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "command ?arg arg...?");
return TCL_ERROR;
}
oldLevel = Itcl_Protection(interp, pLevel);
if (objc == 2) {
result = Tcl_EvalObj(interp, objv[1]);
} else {
result = Itcl_EvalArgs(interp, objc-1, objv+1);
}
if (result == TCL_BREAK) {
Tcl_SetResult(interp, "invoked \"break\" outside of a loop",
TCL_STATIC);
result = TCL_ERROR;
}
else if (result == TCL_CONTINUE) {
Tcl_SetResult(interp, "invoked \"continue\" outside of a loop",
TCL_STATIC);
result = TCL_ERROR;
}
else if (result != TCL_OK) {
char mesg[256], *name;
name = Tcl_GetStringFromObj(objv[0], (int*)NULL);
sprintf(mesg, "\n (%.100s body line %d)",
name, interp->errorLine);
Tcl_AddErrorInfo(interp, mesg);
}
Itcl_Protection(interp, oldLevel);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_DelClassCmd()
*
* Part of the "delete" ensemble. Invoked by Tcl whenever the
* user issues a "delete class" command to delete classes.
* Handles the following syntax:
*
* delete class <name> ?<name>...?
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_DelClassCmd(clientData, interp, objc, objv)
ClientData clientData; /* unused */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int i;
char *name;
ItclClass *cdefn;
/*
* Since destroying a base class will destroy all derived
* classes, calls like "destroy class Base Derived" could
* fail. Break this into two passes: first check to make
* sure that all classes on the command line are valid,
* then delete them.
*/
for (i=1; i < objc; i++) {
name = Tcl_GetStringFromObj(objv[i], (int*)NULL);
cdefn = Itcl_FindClass(interp, name, /* autoload */ 1);
if (cdefn == NULL) {
return TCL_ERROR;
}
}
for (i=1; i < objc; i++) {
name = Tcl_GetStringFromObj(objv[i], (int*)NULL);
cdefn = Itcl_FindClass(interp, name, /* autoload */ 0);
if (cdefn) {
Tcl_ResetResult(interp);
if (Itcl_DeleteClass(interp, cdefn) != TCL_OK) {
return TCL_ERROR;
}
}
}
Tcl_ResetResult(interp);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_DelObjectCmd()
*
* Part of the "delete" ensemble. Invoked by Tcl whenever the user
* issues a "delete object" command to delete [incr Tcl] objects.
* Handles the following syntax:
*
* delete object <name> ?<name>...?
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
int
Itcl_DelObjectCmd(clientData, interp, objc, objv)
ClientData clientData; /* object management info */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int i;
char *name;
ItclObject *contextObj;
/*
* Scan through the list of objects and attempt to delete them.
* If anything goes wrong (i.e., destructors fail), then
* abort with an error.
*/
for (i=1; i < objc; i++) {
name = Tcl_GetStringFromObj(objv[i], (int*)NULL);
if (Itcl_FindObject(interp, name, &contextObj) != TCL_OK) {
return TCL_ERROR;
}
if (contextObj == NULL) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"object \"", name, "\" not found",
(char*)NULL);
return TCL_ERROR;
}
if (Itcl_DeleteObject(interp, contextObj) != TCL_OK) {
return TCL_ERROR;
}
}
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_ScopeCmd()
*
* Invoked by Tcl whenever the user issues a "scope" command to
* create a fully qualified variable name. Handles the following
* syntax:
*
* scope <variable>
*
* If the input string is already fully qualified (starts with "::"),
* then this procedure does nothing. Otherwise, it looks for a
* data member called <variable> and returns its fully qualified
* name. If the <variable> is a common data member, this procedure
* returns a name of the form:
*
* ::namesp::namesp::class::variable
*
* If the <variable> is an instance variable, this procedure returns
* a name of the form:
*
* @itcl ::namesp::namesp::object variable
*
* This kind of scoped value is recognized by the Itcl_ScopedVarResolver
* proc, which handles variable resolution for the entire interpreter.
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_ScopeCmd(dummy, interp, objc, objv)
ClientData dummy; /* unused */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int result = TCL_OK;
Tcl_Namespace *contextNs = Tcl_GetCurrentNamespace(interp);
char *openParen = NULL;
register char *p;
char *token;
ItclClass *contextClass;
ItclObject *contextObj;
ItclObjectInfo *info;
Tcl_CallFrame *framePtr;
Tcl_HashEntry *entry;
ItclVarLookup *vlookup;
Tcl_Obj *objPtr;
Tcl_Var var;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "varname");
return TCL_ERROR;
}
/*
* If this looks like a fully qualified name already,
* then return it as is.
*/
token = Tcl_GetStringFromObj(objv[1], (int*)NULL);
if (*token == ':' && *(token+1) == ':') {
Tcl_SetObjResult(interp, objv[1]);
return TCL_OK;
}
/*
* If the variable name is an array reference, pick out
* the array name and use that for the lookup operations
* below.
*/
for (p=token; *p != '\0'; p++) {
if (*p == '(') {
openParen = p;
}
else if (*p == ')' && openParen) {
*openParen = '\0';
break;
}
}
/*
* Figure out what context we're in. If this is a class,
* then look up the variable in the class definition.
* If this is a namespace, then look up the variable in its
* varTable. Note that the normal Itcl_GetContext function
* returns an error if we're not in a class context, so we
* perform a similar function here, the hard way.
*
* TRICKY NOTE: If this is an array reference, we'll get
* the array variable as the variable name. We must be
* careful to add the index (everything from openParen
* onward) as well.
*/
if (Itcl_IsClassNamespace(contextNs)) {
contextClass = (ItclClass*)contextNs->clientData;
entry = Tcl_FindHashEntry(&contextClass->resolveVars, token);
if (!entry) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"variable \"", token, "\" not found in class \"",
contextClass->fullname, "\"",
(char*)NULL);
result = TCL_ERROR;
goto scopeCmdDone;
}
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (vlookup->vdefn->member->flags & ITCL_COMMON) {
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
Tcl_AppendToObj(resultPtr, vlookup->vdefn->member->fullname, -1);
if (openParen) {
*openParen = '(';
Tcl_AppendToObj(resultPtr, openParen, -1);
openParen = NULL;
}
result = TCL_OK;
goto scopeCmdDone;
}
/*
* If this is not a common variable, then we better have
* an object context. Return the name "@itcl object variable".
*/
framePtr = _Tcl_GetCallFrame(interp, 0);
info = contextClass->info;
entry = Tcl_FindHashEntry(&info->contextFrames, (char*)framePtr);
if (!entry) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"can't scope variable \"", token,
"\": missing object context\"",
(char*)NULL);
result = TCL_ERROR;
goto scopeCmdDone;
}
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
Tcl_AppendElement(interp, "@itcl");
objPtr = Tcl_NewStringObj((char*)NULL, 0);
Tcl_IncrRefCount(objPtr);
Tcl_GetCommandFullName(interp, contextObj->accessCmd, objPtr);
Tcl_AppendElement(interp, Tcl_GetStringFromObj(objPtr, (int*)NULL));
Tcl_DecrRefCount(objPtr);
objPtr = Tcl_NewStringObj((char*)NULL, 0);
Tcl_IncrRefCount(objPtr);
Tcl_AppendToObj(objPtr, vlookup->vdefn->member->fullname, -1);
if (openParen) {
*openParen = '(';
Tcl_AppendToObj(objPtr, openParen, -1);
openParen = NULL;
}
Tcl_AppendElement(interp, Tcl_GetStringFromObj(objPtr, (int*)NULL));
Tcl_DecrRefCount(objPtr);
}
/*
* We must be in an ordinary namespace context. Resolve
* the variable using Tcl_FindNamespaceVar.
*
* TRICKY NOTE: If this is an array reference, we'll get
* the array variable as the variable name. We must be
* careful to add the index (everything from openParen
* onward) as well.
*/
else {
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
var = Tcl_FindNamespaceVar(interp, token, contextNs,
TCL_NAMESPACE_ONLY);
if (!var) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"variable \"", token, "\" not found in namespace \"",
contextNs->fullName, "\"",
(char*)NULL);
result = TCL_ERROR;
goto scopeCmdDone;
}
Tcl_GetVariableFullName(interp, var, resultPtr);
if (openParen) {
*openParen = '(';
Tcl_AppendToObj(resultPtr, openParen, -1);
openParen = NULL;
}
}
scopeCmdDone:
if (openParen) {
*openParen = '(';
}
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_CodeCmd()
*
* Invoked by Tcl whenever the user issues a "code" command to
* create a scoped command string. Handles the following syntax:
*
* code ?-namespace foo? arg ?arg arg ...?
*
* Unlike the scope command, the code command DOES NOT look for
* scoping information at the beginning of the command. So scopes
* will nest in the code command.
*
* The code command is similar to the "namespace code" command in
* Tcl, but it preserves the list structure of the input arguments,
* so it is a lot more useful.
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_CodeCmd(dummy, interp, objc, objv)
ClientData dummy; /* unused */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
Tcl_Namespace *contextNs = Tcl_GetCurrentNamespace(interp);
int pos;
char *token;
Tcl_Obj *listPtr, *objPtr;
/*
* Handle flags like "-namespace"...
*/
for (pos=1; pos < objc; pos++) {
token = Tcl_GetStringFromObj(objv[pos], (int*)NULL);
if (*token != '-') {
break;
}
if (strcmp(token, "-namespace") == 0) {
if (objc == 2) {
Tcl_WrongNumArgs(interp, 1, objv,
"?-namespace name? command ?arg arg...?");
return TCL_ERROR;
} else {
token = Tcl_GetStringFromObj(objv[pos+1], (int*)NULL);
contextNs = Tcl_FindNamespace(interp, token,
(Tcl_Namespace*)NULL, TCL_LEAVE_ERR_MSG);
if (!contextNs) {
return TCL_ERROR;
}
pos++;
}
}
else if (strcmp(token, "--") == 0) {
pos++;
break;
}
else {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"bad option \"", token, "\": should be -namespace or --",
(char*)NULL);
return TCL_ERROR;
}
}
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv,
"?-namespace name? command ?arg arg...?");
return TCL_ERROR;
}
/*
* Now construct a scoped command by integrating the
* current namespace context, and appending the remaining
* arguments AS A LIST...
*/
listPtr = Tcl_NewListObj(0, (Tcl_Obj**)NULL);
Tcl_ListObjAppendElement(interp, listPtr,
Tcl_NewStringObj("namespace", -1));
Tcl_ListObjAppendElement(interp, listPtr,
Tcl_NewStringObj("inscope", -1));
if (contextNs == Tcl_GetGlobalNamespace(interp)) {
objPtr = Tcl_NewStringObj("::", -1);
} else {
objPtr = Tcl_NewStringObj(contextNs->fullName, -1);
}
Tcl_ListObjAppendElement(interp, listPtr, objPtr);
if (objc-pos == 1) {
objPtr = objv[pos];
} else {
objPtr = Tcl_NewListObj(objc-pos, &objv[pos]);
}
Tcl_ListObjAppendElement(interp, listPtr, objPtr);
Tcl_SetObjResult(interp, listPtr);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_StubCreateCmd()
*
* Invoked by Tcl whenever the user issues a "stub create" command to
* create an autoloading stub for imported commands. Handles the
* following syntax:
*
* stub create <name>
*
* Creates a command called <name>. Executing this command will cause
* the real command <name> to be autoloaded.
* ------------------------------------------------------------------------
*/
int
Itcl_StubCreateCmd(clientData, interp, objc, objv)
ClientData clientData; /* not used */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
char *cmdName;
Command *cmdPtr;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "name");
return TCL_ERROR;
}
cmdName = Tcl_GetStringFromObj(objv[1], (int*)NULL);
/*
* Create a stub command with the characteristic ItclDeleteStub
* procedure. That way, we can recognize this command later
* on as a stub. Save the cmd token as client data, so we can
* get the full name of this command later on.
*/
cmdPtr = (Command *) Tcl_CreateObjCommand(interp, cmdName,
ItclHandleStubCmd, (ClientData)NULL,
(Tcl_CmdDeleteProc*)ItclDeleteStub);
cmdPtr->objClientData = (ClientData) cmdPtr;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_StubExistsCmd()
*
* Invoked by Tcl whenever the user issues a "stub exists" command to
* see if an existing command is an autoloading stub. Handles the
* following syntax:
*
* stub exists <name>
*
* Looks for a command called <name> and checks to see if it is an
* autoloading stub. Returns a boolean result.
* ------------------------------------------------------------------------
*/
int
Itcl_StubExistsCmd(clientData, interp, objc, objv)
ClientData clientData; /* not used */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
char *cmdName;
Tcl_Command cmd;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "name");
return TCL_ERROR;
}
cmdName = Tcl_GetStringFromObj(objv[1], (int*)NULL);
cmd = Tcl_FindCommand(interp, cmdName, (Tcl_Namespace*)NULL, 0);
if (cmd != NULL && Itcl_IsStub(cmd)) {
Tcl_SetIntObj(Tcl_GetObjResult(interp), 1);
} else {
Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
}
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_IsStub()
*
* Checks the given Tcl command to see if it represents an autoloading
* stub created by the "stub create" command. Returns non-zero if
* the command is indeed a stub.
* ------------------------------------------------------------------------
*/
int
Itcl_IsStub(cmd)
Tcl_Command cmd; /* command being tested */
{
Command *cmdPtr = (Command*)cmd;
/*
* This may be an imported command, but don't try to get the
* original. Just check to see if this particular command
* is a stub. If we really want the original command, we'll
* find it at a higher level.
*/
if (cmdPtr->deleteProc == ItclDeleteStub) {
return 1;
}
return 0;
}
/*
* ------------------------------------------------------------------------
* ItclHandleStubCmd()
*
* Invoked by Tcl to handle commands created by "stub create".
* Calls "auto_load" with the full name of the current command to
* trigger autoloading of the real implementation. Then, calls the
* command to handle its function. If successful, this command
* returns TCL_OK along with the result from the real implementation
* of this command. Otherwise, it returns TCL_ERROR, along with an
* error message in the interpreter.
* ------------------------------------------------------------------------
*/
static int
ItclHandleStubCmd(clientData, interp, objc, objv)
ClientData clientData; /* command token for this stub */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
Tcl_Command cmd = (Tcl_Command) clientData;
int result, loaded;
char *cmdName;
int cmdlinec;
Tcl_Obj **cmdlinev;
Tcl_Obj *objAutoLoad[2], *objPtr, *cmdNamePtr, *cmdlinePtr;
cmdNamePtr = Tcl_NewStringObj((char*)NULL, 0);
Tcl_GetCommandFullName(interp, cmd, cmdNamePtr);
Tcl_IncrRefCount(cmdNamePtr);
cmdName = Tcl_GetStringFromObj(cmdNamePtr, (int*)NULL);
/*
* Try to autoload the real command for this stub.
*/
objAutoLoad[0] = Tcl_NewStringObj("::auto_load", -1);
Tcl_IncrRefCount(objAutoLoad[0]);
objAutoLoad[1] = cmdNamePtr;
Tcl_IncrRefCount(objAutoLoad[1]);
result = Itcl_EvalArgs(interp, 2, objAutoLoad);
Tcl_DecrRefCount(objAutoLoad[0]);
Tcl_DecrRefCount(objAutoLoad[1]);
if (result != TCL_OK) {
Tcl_DecrRefCount(cmdNamePtr);
return TCL_ERROR;
}
objPtr = Tcl_GetObjResult(interp);
result = Tcl_GetIntFromObj(interp, objPtr, &loaded);
if (result != TCL_OK || !loaded) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"can't autoload \"", cmdName, "\"", (char*)NULL);
Tcl_DecrRefCount(cmdNamePtr);
return TCL_ERROR;
}
/*
* At this point, the real implementation has been loaded.
* Invoke the command again with the arguments passed in.
*/
cmdlinePtr = Itcl_CreateArgs(interp, cmdName, objc-1, objv+1);
(void) Tcl_ListObjGetElements((Tcl_Interp*)NULL, cmdlinePtr,
&cmdlinec, &cmdlinev);
Tcl_ResetResult(interp);
result = Itcl_EvalArgs(interp, cmdlinec, cmdlinev);
Tcl_DecrRefCount(cmdlinePtr);
return result;
}
/*
* ------------------------------------------------------------------------
* ItclDeleteStub()
*
* Invoked by Tcl whenever a stub command is deleted. This procedure
* does nothing, but its presence identifies a command as a stub.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
static void
ItclDeleteStub(cdata)
ClientData cdata; /* not used */
{
/* do nothing */
}
|