00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #define _GLOBLDEF_SOURCE_
00030
00031 #include "setup.h"
00032
00033 #if DEFGLOBAL_CONSTRUCT
00034
00035 #include <stdio.h>
00036 #define _STDIO_INCLUDED_
00037
00038 #include "memalloc.h"
00039 #include "modulpsr.h"
00040 #include "multifld.h"
00041 #include "router.h"
00042 #include "strngrtr.h"
00043 #include "modulutl.h"
00044 #include "globlbsc.h"
00045 #include "globlpsr.h"
00046 #include "globlcom.h"
00047 #include "utility.h"
00048 #include "commline.h"
00049 #include "envrnmnt.h"
00050
00051 #if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
00052 #include "bload.h"
00053 #include "globlbin.h"
00054 #endif
00055
00056 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00057 #include "globlcmp.h"
00058 #endif
00059
00060 #include "globldef.h"
00061
00062
00063
00064
00065
00066 static void *AllocateModule(void *);
00067 static void ReturnModule(void *,void *);
00068 static void ReturnDefglobal(void *,void *);
00069 static void InitializeDefglobalModules(void *);
00070 static intBool GetDefglobalValue2(void *,void *,DATA_OBJECT_PTR);
00071 static void IncrementDefglobalBusyCount(void *,void *);
00072 static void DecrementDefglobalBusyCount(void *,void *);
00073 static void DeallocateDefglobalData(void *);
00074 static void DestroyDefglobalAction(void *,struct constructHeader *,void *);
00075 static void DestroyDefglobal(void *,void *);
00076
00077
00078
00079
00080 globle void InitializeDefglobals(
00081 void *theEnv)
00082 {
00083 struct entityRecord globalInfo = { "GBL_VARIABLE", GBL_VARIABLE,0,0,0,
00084 NULL,
00085 NULL,
00086 NULL,
00087 GetDefglobalValue2,
00088 NULL,NULL,
00089 NULL,NULL,NULL,NULL,NULL,NULL };
00090
00091 struct entityRecord defglobalPtrRecord = { "DEFGLOBAL_PTR", DEFGLOBAL_PTR,0,0,0,
00092 NULL,NULL,NULL,
00093 QGetDefglobalValue,
00094 NULL,
00095 DecrementDefglobalBusyCount,
00096 IncrementDefglobalBusyCount,
00097 NULL,NULL,NULL,NULL,NULL };
00098
00099 AllocateEnvironmentData(theEnv,DEFGLOBAL_DATA,sizeof(struct defglobalData),DeallocateDefglobalData);
00100
00101 memcpy(&DefglobalData(theEnv)->GlobalInfo,&globalInfo,sizeof(struct entityRecord));
00102 memcpy(&DefglobalData(theEnv)->DefglobalPtrRecord,&defglobalPtrRecord,sizeof(struct entityRecord));
00103
00104 DefglobalData(theEnv)->ResetGlobals = TRUE;
00105 DefglobalData(theEnv)->LastModuleIndex = -1;
00106
00107 InstallPrimitive(theEnv,&DefglobalData(theEnv)->GlobalInfo,GBL_VARIABLE);
00108 InstallPrimitive(theEnv,&DefglobalData(theEnv)->DefglobalPtrRecord,DEFGLOBAL_PTR);
00109
00110 InitializeDefglobalModules(theEnv);
00111
00112 DefglobalBasicCommands(theEnv);
00113 DefglobalCommandDefinitions(theEnv);
00114
00115 DefglobalData(theEnv)->DefglobalConstruct =
00116 AddConstruct(theEnv,"defglobal","defglobals",ParseDefglobal,EnvFindDefglobal,
00117 GetConstructNamePointer,GetConstructPPForm,
00118 GetConstructModuleItem,EnvGetNextDefglobal,SetNextConstruct,
00119 EnvIsDefglobalDeletable,EnvUndefglobal,ReturnDefglobal);
00120 }
00121
00122
00123
00124
00125
00126 static void DeallocateDefglobalData(
00127 void *theEnv)
00128 {
00129 #if ! RUN_TIME
00130 struct defglobalModule *theModuleItem;
00131 void *theModule;
00132
00133 #if BLOAD || BLOAD_AND_BSAVE
00134 if (Bloaded(theEnv)) return;
00135 #endif
00136
00137 DoForAllConstructs(theEnv,DestroyDefglobalAction,DefglobalData(theEnv)->DefglobalModuleIndex,FALSE,NULL);
00138
00139 for (theModule = EnvGetNextDefmodule(theEnv,NULL);
00140 theModule != NULL;
00141 theModule = EnvGetNextDefmodule(theEnv,theModule))
00142 {
00143 theModuleItem = (struct defglobalModule *)
00144 GetModuleItem(theEnv,(struct defmodule *) theModule,
00145 DefglobalData(theEnv)->DefglobalModuleIndex);
00146 rtn_struct(theEnv,defglobalModule,theModuleItem);
00147 }
00148 #else
00149 DoForAllConstructs(theEnv,DestroyDefglobalAction,DefglobalData(theEnv)->DefglobalModuleIndex,FALSE,NULL);
00150 #endif
00151 }
00152
00153
00154
00155
00156
00157 #if WIN_BTC
00158 #pragma argsused
00159 #endif
00160 static void DestroyDefglobalAction(
00161 void *theEnv,
00162 struct constructHeader *theConstruct,
00163 void *buffer)
00164 {
00165 #if MAC_MCW || WIN_MCW || MAC_XCD
00166 #pragma unused(buffer)
00167 #endif
00168 #if (! BLOAD_ONLY)
00169 struct defglobal *theDefglobal = (struct defglobal *) theConstruct;
00170
00171 if (theDefglobal == NULL) return;
00172
00173 DestroyDefglobal(theEnv,theDefglobal);
00174 #else
00175 #if MAC_MCW || WIN_MCW || MAC_XCD
00176 #pragma unused(theEnv,theConstruct)
00177 #endif
00178 #endif
00179 }
00180
00181
00182
00183
00184
00185 static void InitializeDefglobalModules(
00186 void *theEnv)
00187 {
00188 DefglobalData(theEnv)->DefglobalModuleIndex = RegisterModuleItem(theEnv,"defglobal",
00189 AllocateModule,
00190 ReturnModule,
00191 #if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
00192 BloadDefglobalModuleReference,
00193 #else
00194 NULL,
00195 #endif
00196 #if CONSTRUCT_COMPILER && (! RUN_TIME)
00197 DefglobalCModuleReference,
00198 #else
00199 NULL,
00200 #endif
00201 EnvFindDefglobal);
00202
00203 #if (! BLOAD_ONLY) && (! RUN_TIME) && DEFMODULE_CONSTRUCT
00204 AddPortConstructItem(theEnv,"defglobal",SYMBOL);
00205 #endif
00206 }
00207
00208
00209
00210
00211 static void *AllocateModule(
00212 void *theEnv)
00213 {
00214 return((void *) get_struct(theEnv,defglobalModule));
00215 }
00216
00217
00218
00219
00220 static void ReturnModule(
00221 void *theEnv,
00222 void *theItem)
00223 {
00224 FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DefglobalData(theEnv)->DefglobalConstruct);
00225 rtn_struct(theEnv,defglobalModule,theItem);
00226 }
00227
00228
00229
00230
00231
00232 globle struct defglobalModule *GetDefglobalModuleItem(
00233 void *theEnv,
00234 struct defmodule *theModule)
00235 {
00236 return((struct defglobalModule *) GetConstructModuleItemByIndex(theEnv,theModule,DefglobalData(theEnv)->DefglobalModuleIndex));
00237 }
00238
00239
00240
00241
00242
00243
00244 globle void *EnvFindDefglobal(
00245 void *theEnv,
00246 char *defglobalName)
00247 {
00248 return(FindNamedConstruct(theEnv,defglobalName,DefglobalData(theEnv)->DefglobalConstruct));
00249 }
00250
00251
00252
00253
00254
00255
00256 globle void *EnvGetNextDefglobal(
00257 void *theEnv,
00258 void *defglobalPtr)
00259 {
00260 return((void *) GetNextConstructItem(theEnv,(struct constructHeader *) defglobalPtr,DefglobalData(theEnv)->DefglobalModuleIndex));
00261 }
00262
00263
00264
00265
00266
00267 globle intBool EnvIsDefglobalDeletable(
00268 void *theEnv,
00269 void *ptr)
00270 {
00271 if (! ConstructsDeletable(theEnv))
00272 { return FALSE; }
00273
00274 if (((struct defglobal *) ptr)->busyCount) return(FALSE);
00275
00276 return(TRUE);
00277 }
00278
00279
00280
00281
00282
00283 static void ReturnDefglobal(
00284 void *theEnv,
00285 void *vTheDefglobal)
00286 {
00287 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00288 #pragma unused(theEnv,vTheDefglobal)
00289 #endif
00290
00291 #if (! BLOAD_ONLY) && (! RUN_TIME)
00292 struct defglobal *theDefglobal = (struct defglobal *) vTheDefglobal;
00293
00294 if (theDefglobal == NULL) return;
00295
00296
00297
00298
00299
00300 ValueDeinstall(theEnv,&theDefglobal->current);
00301 if (theDefglobal->current.type == MULTIFIELD)
00302 { ReturnMultifield(theEnv,(struct multifield *) theDefglobal->current.value); }
00303
00304
00305
00306
00307
00308
00309 RemoveHashedExpression(theEnv,theDefglobal->initial);
00310
00311
00312
00313
00314
00315
00316 DeinstallConstructHeader(theEnv,&theDefglobal->header);
00317
00318
00319
00320
00321
00322 rtn_struct(theEnv,defglobal,theDefglobal);
00323
00324
00325
00326
00327
00328
00329 DefglobalData(theEnv)->ChangeToGlobals = TRUE;
00330 #endif
00331 }
00332
00333
00334
00335
00336
00337 static void DestroyDefglobal(
00338 void *theEnv,
00339 void *vTheDefglobal)
00340 {
00341 #if (MAC_MCW || WIN_MCW) && BLOAD_ONLY
00342 #pragma unused(theEnv,vTheDefglobal)
00343 #endif
00344
00345 #if (! BLOAD_ONLY)
00346 struct defglobal *theDefglobal = (struct defglobal *) vTheDefglobal;
00347
00348 if (theDefglobal == NULL) return;
00349
00350
00351
00352
00353
00354 if (theDefglobal->current.type == MULTIFIELD)
00355 { ReturnMultifield(theEnv,(struct multifield *) theDefglobal->current.value); }
00356
00357 #if (! RUN_TIME)
00358
00359
00360
00361
00362
00363
00364 DeinstallConstructHeader(theEnv,&theDefglobal->header);
00365
00366
00367
00368
00369
00370 rtn_struct(theEnv,defglobal,theDefglobal);
00371 #endif
00372 #endif
00373 }
00374
00375
00376
00377
00378
00379 globle void QSetDefglobalValue(
00380 void *theEnv,
00381 struct defglobal *theGlobal,
00382 DATA_OBJECT_PTR vPtr,
00383 int resetVar)
00384 {
00385
00386
00387
00388
00389
00390
00391 if (resetVar)
00392 {
00393 EvaluateExpression(theEnv,theGlobal->initial,vPtr);
00394 if (EvaluationData(theEnv)->EvaluationError)
00395 {
00396 vPtr->type = SYMBOL;
00397 vPtr->value = EnvFalseSymbol(theEnv);
00398 }
00399 }
00400
00401
00402
00403
00404
00405
00406 #if DEBUGGING_FUNCTIONS
00407 if (theGlobal->watch)
00408 {
00409 EnvPrintRouter(theEnv,WTRACE,":== ?*");
00410 EnvPrintRouter(theEnv,WTRACE,ValueToString(theGlobal->header.name));
00411 EnvPrintRouter(theEnv,WTRACE,"* ==> ");
00412 PrintDataObject(theEnv,WTRACE,vPtr);
00413 EnvPrintRouter(theEnv,WTRACE," <== ");
00414 PrintDataObject(theEnv,WTRACE,&theGlobal->current);
00415 EnvPrintRouter(theEnv,WTRACE,"\n");
00416 }
00417 #endif
00418
00419
00420
00421
00422
00423 ValueDeinstall(theEnv,&theGlobal->current);
00424 if (theGlobal->current.type == MULTIFIELD)
00425 { ReturnMultifield(theEnv,(struct multifield *) theGlobal->current.value); }
00426
00427
00428
00429
00430
00431 theGlobal->current.type = vPtr->type;
00432 if (vPtr->type != MULTIFIELD) theGlobal->current.value = vPtr->value;
00433 else DuplicateMultifield(theEnv,&theGlobal->current,vPtr);
00434 ValueInstall(theEnv,&theGlobal->current);
00435
00436
00437
00438
00439
00440
00441 DefglobalData(theEnv)->ChangeToGlobals = TRUE;
00442
00443 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00444 (EvaluationData(theEnv)->CurrentExpression == NULL))
00445 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00446 }
00447
00448
00449
00450
00451
00452
00453 globle struct defglobal *QFindDefglobal(
00454 void *theEnv,
00455 SYMBOL_HN *defglobalName)
00456 {
00457 struct defglobal *theDefglobal;
00458
00459 for (theDefglobal = (struct defglobal *) EnvGetNextDefglobal(theEnv,NULL);
00460 theDefglobal != NULL;
00461 theDefglobal = (struct defglobal *) EnvGetNextDefglobal(theEnv,theDefglobal))
00462 { if (defglobalName == theDefglobal->header.name) return (theDefglobal); }
00463
00464 return(NULL);
00465 }
00466
00467
00468
00469
00470
00471
00472
00473 globle void EnvGetDefglobalValueForm(
00474 void *theEnv,
00475 char *buffer,
00476 unsigned bufferLength,
00477 void *vTheGlobal)
00478 {
00479 struct defglobal *theGlobal = (struct defglobal *) vTheGlobal;
00480
00481 OpenStringDestination(theEnv,"GlobalValueForm",buffer,bufferLength);
00482 EnvPrintRouter(theEnv,"GlobalValueForm","?*");
00483 EnvPrintRouter(theEnv,"GlobalValueForm",ValueToString(theGlobal->header.name));
00484 EnvPrintRouter(theEnv,"GlobalValueForm","* = ");
00485 PrintDataObject(theEnv,"GlobalValueForm",&theGlobal->current);
00486 CloseStringDestination(theEnv,"GlobalValueForm");
00487 }
00488
00489
00490
00491
00492 globle int EnvGetGlobalsChanged(
00493 void *theEnv)
00494 {
00495 return(DefglobalData(theEnv)->ChangeToGlobals);
00496 }
00497
00498
00499
00500
00501 globle void EnvSetGlobalsChanged(
00502 void *theEnv,
00503 int value)
00504 {
00505 DefglobalData(theEnv)->ChangeToGlobals = value;
00506 }
00507
00508
00509
00510
00511
00512 static intBool GetDefglobalValue2(
00513 void *theEnv,
00514 void *theValue,
00515 DATA_OBJECT_PTR vPtr)
00516 {
00517 struct defglobal *theGlobal;
00518 int count;
00519
00520
00521
00522
00523
00524
00525 theGlobal = (struct defglobal *)
00526 FindImportedConstruct(theEnv,"defglobal",NULL,ValueToString(theValue),
00527 &count,TRUE,NULL);
00528
00529
00530
00531
00532
00533 if (theGlobal == NULL)
00534 {
00535 PrintErrorID(theEnv,"GLOBLDEF",1,FALSE);
00536 EnvPrintRouter(theEnv,WERROR,"Global variable ?*");
00537 EnvPrintRouter(theEnv,WERROR,ValueToString(theValue));
00538 EnvPrintRouter(theEnv,WERROR,"* is unbound.\n");
00539 vPtr->type = SYMBOL;
00540 vPtr->value = EnvFalseSymbol(theEnv);
00541 SetEvaluationError(theEnv,TRUE);
00542 return(FALSE);
00543 }
00544
00545
00546
00547
00548
00549
00550
00551 if (count > 1)
00552 {
00553 AmbiguousReferenceErrorMessage(theEnv,"defglobal",ValueToString(theValue));
00554 vPtr->type = SYMBOL;
00555 vPtr->value = EnvFalseSymbol(theEnv);
00556 SetEvaluationError(theEnv,TRUE);
00557 return(FALSE);
00558 }
00559
00560
00561
00562
00563
00564 QGetDefglobalValue(theEnv,theGlobal,vPtr);
00565
00566 return(TRUE);
00567 }
00568
00569
00570
00571
00572 globle int QGetDefglobalValue(
00573 void *theEnv,
00574 void *vTheGlobal,
00575 DATA_OBJECT_PTR vPtr)
00576 {
00577 struct defglobal *theGlobal = (struct defglobal *) vTheGlobal;
00578
00579
00580
00581
00582
00583 vPtr->type = theGlobal->current.type;
00584 vPtr->value = theGlobal->current.value;
00585 vPtr->begin = theGlobal->current.begin;
00586 vPtr->end = theGlobal->current.end;
00587
00588
00589
00590
00591
00592
00593
00594 if (vPtr->type == MULTIFIELD)
00595 {
00596 vPtr->value = EnvCreateMultifield(theEnv,(unsigned long) (vPtr->end + 1));
00597 GenCopyMemory(struct field,vPtr->end + 1,
00598 &((struct multifield *) vPtr->value)->theFields[0],
00599 &((struct multifield *) theGlobal->current.value)->theFields[theGlobal->current.begin]);
00600 }
00601
00602 return(TRUE);
00603 }
00604
00605
00606
00607
00608
00609 globle intBool EnvGetDefglobalValue(
00610 void *theEnv,
00611 char *variableName,
00612 DATA_OBJECT_PTR vPtr)
00613 {
00614 struct defglobal *theDefglobal;
00615
00616 if ((theDefglobal = (struct defglobal *) EnvFindDefglobal(theEnv,variableName)) == NULL)
00617 { return(FALSE); }
00618
00619 QGetDefglobalValue(theEnv,theDefglobal,vPtr);
00620
00621 return(TRUE);
00622 }
00623
00624
00625
00626
00627
00628 globle intBool EnvSetDefglobalValue(
00629 void *theEnv,
00630 char *variableName,
00631 DATA_OBJECT_PTR vPtr)
00632 {
00633 struct defglobal *theGlobal;
00634
00635 if ((theGlobal = QFindDefglobal(theEnv,(SYMBOL_HN *) EnvAddSymbol(theEnv,variableName))) == NULL)
00636 { return(FALSE); }
00637
00638 QSetDefglobalValue(theEnv,theGlobal,vPtr,FALSE);
00639
00640 return(TRUE);
00641 }
00642
00643
00644
00645
00646
00647 static void DecrementDefglobalBusyCount(
00648 void *theEnv,
00649 void *vTheGlobal)
00650 {
00651 struct defglobal *theGlobal = (struct defglobal *) vTheGlobal;
00652
00653 if (! ConstructData(theEnv)->ClearInProgress) theGlobal->busyCount--;
00654 }
00655
00656
00657
00658
00659
00660 #if WIN_BTC
00661 #pragma argsused
00662 #endif
00663 static void IncrementDefglobalBusyCount(
00664 void *theEnv,
00665 void *vTheGlobal)
00666 {
00667 struct defglobal *theGlobal = (struct defglobal *) vTheGlobal;
00668 #if MAC_MCW || WIN_MCW || MAC_XCD
00669 #pragma unused(theEnv)
00670 #endif
00671
00672 theGlobal->busyCount++;
00673 }
00674
00675
00676
00677
00678 globle void UpdateDefglobalScope(
00679 void *theEnv)
00680 {
00681 struct defglobal *theDefglobal;
00682 int moduleCount;
00683 struct defmodule *theModule;
00684 struct defmoduleItemHeader *theItem;
00685
00686
00687
00688
00689
00690 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00691 theModule != NULL;
00692 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00693 {
00694
00695
00696
00697
00698 theItem = (struct defmoduleItemHeader *)
00699 GetModuleItem(theEnv,theModule,DefglobalData(theEnv)->DefglobalModuleIndex);
00700
00701 for (theDefglobal = (struct defglobal *) theItem->firstItem;
00702 theDefglobal != NULL ;
00703 theDefglobal = (struct defglobal *) EnvGetNextDefglobal(theEnv,theDefglobal))
00704 {
00705
00706
00707
00708
00709
00710
00711 if (FindImportedConstruct(theEnv,"defglobal",theModule,
00712 ValueToString(theDefglobal->header.name),
00713 &moduleCount,TRUE,NULL) != NULL)
00714 { theDefglobal->inScope = TRUE; }
00715 else
00716 { theDefglobal->inScope = FALSE; }
00717 }
00718 }
00719 }
00720
00721
00722
00723
00724
00725
00726
00727 globle void *GetNextDefglobalInScope(
00728 void *theEnv,
00729 void *vTheGlobal)
00730 {
00731 struct defglobal *theGlobal = (struct defglobal *) vTheGlobal;
00732 struct defmoduleItemHeader *theItem;
00733
00734
00735
00736
00737
00738
00739 if (theGlobal == NULL)
00740 {
00741
00742
00743
00744
00745
00746
00747 if (DefglobalData(theEnv)->LastModuleIndex != DefmoduleData(theEnv)->ModuleChangeIndex)
00748 {
00749 UpdateDefglobalScope(theEnv);
00750 DefglobalData(theEnv)->LastModuleIndex = DefmoduleData(theEnv)->ModuleChangeIndex;
00751 }
00752
00753
00754
00755
00756
00757
00758 DefglobalData(theEnv)->TheDefmodule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00759 theItem = (struct defmoduleItemHeader *)
00760 GetModuleItem(theEnv,DefglobalData(theEnv)->TheDefmodule,DefglobalData(theEnv)->DefglobalModuleIndex);
00761 theGlobal = (struct defglobal *) theItem->firstItem;
00762 }
00763
00764
00765
00766
00767
00768
00769 else
00770 { theGlobal = (struct defglobal *) EnvGetNextDefglobal(theEnv,theGlobal); }
00771
00772
00773
00774
00775
00776
00777 while (DefglobalData(theEnv)->TheDefmodule != NULL)
00778 {
00779
00780
00781
00782
00783
00784 for (;
00785 theGlobal != NULL;
00786 theGlobal = (struct defglobal *) EnvGetNextDefglobal(theEnv,theGlobal))
00787 { if (theGlobal->inScope) return((void *) theGlobal); }
00788
00789
00790
00791
00792
00793
00794 DefglobalData(theEnv)->TheDefmodule = (struct defmodule *) EnvGetNextDefmodule(theEnv,DefglobalData(theEnv)->TheDefmodule);
00795 theItem = (struct defmoduleItemHeader *)
00796 GetModuleItem(theEnv,DefglobalData(theEnv)->TheDefmodule,DefglobalData(theEnv)->DefglobalModuleIndex);
00797 theGlobal = (struct defglobal *) theItem->firstItem;
00798 }
00799
00800
00801
00802
00803
00804
00805 return(NULL);
00806 }
00807
00808 #endif
00809
00810