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 _CONSTRCT_SOURCE_
00030
00031 #include <stdio.h>
00032 #define _STDIO_INCLUDED_
00033 #include <string.h>
00034
00035 #include "setup.h"
00036
00037 #include "constant.h"
00038 #include "envrnmnt.h"
00039 #include "memalloc.h"
00040 #include "router.h"
00041 #include "scanner.h"
00042 #include "watch.h"
00043 #include "prcdrfun.h"
00044 #include "prcdrpsr.h"
00045 #include "argacces.h"
00046 #include "exprnpsr.h"
00047 #include "multifld.h"
00048 #include "moduldef.h"
00049 #include "sysdep.h"
00050 #include "utility.h"
00051 #include "commline.h"
00052
00053 #include "ruledef.h"
00054 #include "constrct.h"
00055
00056
00057
00058
00059
00060 static void DeallocateConstructData(void *);
00061
00062
00063
00064
00065
00066 globle void InitializeConstructData(
00067 void *theEnv)
00068 {
00069 AllocateEnvironmentData(theEnv,CONSTRUCT_DATA,sizeof(struct constructData),DeallocateConstructData);
00070
00071 #if (! RUN_TIME) && (! BLOAD_ONLY)
00072 ConstructData(theEnv)->WatchCompilations = ON;
00073 #endif
00074 }
00075
00076
00077
00078
00079
00080 static void DeallocateConstructData(
00081 void *theEnv)
00082 {
00083 struct construct *tmpPtr, *nextPtr;
00084
00085 #if (! RUN_TIME) && (! BLOAD_ONLY)
00086 DeallocateCallList(theEnv,ConstructData(theEnv)->ListOfSaveFunctions);
00087 #endif
00088 DeallocateCallList(theEnv,ConstructData(theEnv)->ListOfResetFunctions);
00089 DeallocateCallList(theEnv,ConstructData(theEnv)->ListOfClearFunctions);
00090 DeallocateCallList(theEnv,ConstructData(theEnv)->ListOfClearReadyFunctions);
00091
00092 tmpPtr = ConstructData(theEnv)->ListOfConstructs;
00093 while (tmpPtr != NULL)
00094 {
00095 nextPtr = tmpPtr->next;
00096 rtn_struct(theEnv,construct,tmpPtr);
00097 tmpPtr = nextPtr;
00098 }
00099 }
00100
00101 #if (! RUN_TIME) && (! BLOAD_ONLY)
00102
00103
00104
00105
00106
00107 globle struct construct *FindConstruct(
00108 void *theEnv,
00109 char *name)
00110 {
00111 struct construct *currentPtr;
00112
00113 for (currentPtr = ConstructData(theEnv)->ListOfConstructs;
00114 currentPtr != NULL;
00115 currentPtr = currentPtr->next)
00116 {
00117 if (strcmp(name,currentPtr->constructName) == 0)
00118 { return(currentPtr); }
00119 }
00120
00121 return(NULL);
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 globle int RemoveConstruct(
00131 void *theEnv,
00132 char *name)
00133 {
00134 struct construct *currentPtr, *lastPtr = NULL;
00135
00136 for (currentPtr = ConstructData(theEnv)->ListOfConstructs;
00137 currentPtr != NULL;
00138 currentPtr = currentPtr->next)
00139 {
00140 if (strcmp(name,currentPtr->constructName) == 0)
00141 {
00142 if (lastPtr == NULL)
00143 { ConstructData(theEnv)->ListOfConstructs = currentPtr->next; }
00144 else
00145 { lastPtr->next = currentPtr->next; }
00146 rtn_struct(theEnv,construct,currentPtr);
00147 return(TRUE);
00148 }
00149
00150 lastPtr = currentPtr;
00151 }
00152
00153 return(FALSE);
00154 }
00155
00156
00157
00158
00159
00160 #if ALLOW_ENVIRONMENT_GLOBALS
00161 globle int Save(
00162 char *fileName)
00163 {
00164 return EnvSave(GetCurrentEnvironment(),fileName);
00165 }
00166 #endif
00167
00168
00169
00170
00171 globle int EnvSave(
00172 void *theEnv,
00173 char *fileName)
00174 {
00175 struct callFunctionItem *saveFunction;
00176 FILE *filePtr;
00177 void *defmodulePtr;
00178
00179
00180
00181
00182
00183 if ((filePtr = GenOpen(theEnv,fileName,"w")) == NULL)
00184 { return(FALSE); }
00185
00186
00187
00188
00189
00190 SetFastSave(theEnv,filePtr);
00191
00192
00193
00194
00195
00196 for (defmodulePtr = EnvGetNextDefmodule(theEnv,NULL);
00197 defmodulePtr != NULL;
00198 defmodulePtr = EnvGetNextDefmodule(theEnv,defmodulePtr))
00199 {
00200 for (saveFunction = ConstructData(theEnv)->ListOfSaveFunctions;
00201 saveFunction != NULL;
00202 saveFunction = saveFunction->next)
00203 {
00204 ((* (void (*)(void *,void *,char *)) saveFunction->func))(theEnv,defmodulePtr,(char *) filePtr);
00205 }
00206 }
00207
00208
00209
00210
00211
00212 GenClose(theEnv,filePtr);
00213
00214
00215
00216
00217
00218 SetFastSave(theEnv,NULL);
00219
00220
00221
00222
00223
00224
00225 return(TRUE);
00226 }
00227
00228
00229
00230
00231
00232
00233 globle intBool RemoveSaveFunction(
00234 void *theEnv,
00235 char *name)
00236 {
00237 int found;
00238
00239 ConstructData(theEnv)->ListOfSaveFunctions =
00240 RemoveFunctionFromCallList(theEnv,name,ConstructData(theEnv)->ListOfSaveFunctions,&found);
00241
00242 if (found) return(TRUE);
00243
00244 return(FALSE);
00245 }
00246
00247
00248
00249
00250
00251 globle void SetCompilationsWatch(
00252 void *theEnv,
00253 unsigned value)
00254 {
00255 ConstructData(theEnv)->WatchCompilations = value;
00256 }
00257
00258
00259
00260
00261
00262 globle unsigned GetCompilationsWatch(
00263 void *theEnv)
00264 {
00265 return(ConstructData(theEnv)->WatchCompilations);
00266 }
00267
00268
00269
00270
00271
00272 globle void SetPrintWhileLoading(
00273 void *theEnv,
00274 intBool value)
00275 {
00276 ConstructData(theEnv)->PrintWhileLoading = value;
00277 }
00278
00279
00280
00281
00282
00283 globle intBool GetPrintWhileLoading(
00284 void *theEnv)
00285 {
00286 return(ConstructData(theEnv)->PrintWhileLoading);
00287 }
00288 #endif
00289
00290
00291
00292
00293
00294 globle void InitializeConstructs(
00295 void *theEnv)
00296 {
00297 #if (! RUN_TIME)
00298 EnvDefineFunction2(theEnv,"clear", 'v', PTIEF ClearCommand, "ClearCommand", "00");
00299 EnvDefineFunction2(theEnv,"reset", 'v', PTIEF ResetCommand, "ResetCommand", "00");
00300
00301 #if DEBUGGING_FUNCTIONS && (! BLOAD_ONLY)
00302 AddWatchItem(theEnv,"compilations",0,&ConstructData(theEnv)->WatchCompilations,30,NULL,NULL);
00303 #endif
00304 #else
00305 #if MAC_MCW || WIN_MCW || MAC_XCD
00306 #pragma unused(theEnv)
00307 #endif
00308 #endif
00309 }
00310
00311
00312
00313
00314
00315 globle void ClearCommand(
00316 void *theEnv)
00317 {
00318 if (EnvArgCountCheck(theEnv,"clear",EXACTLY,0) == -1) return;
00319 EnvClear(theEnv);
00320 return;
00321 }
00322
00323
00324
00325
00326
00327 globle void ResetCommand(
00328 void *theEnv)
00329 {
00330 if (EnvArgCountCheck(theEnv,"reset",EXACTLY,0) == -1) return;
00331 EnvReset(theEnv);
00332 return;
00333 }
00334
00335
00336
00337
00338
00339 #if ALLOW_ENVIRONMENT_GLOBALS
00340 globle void Reset()
00341 {
00342 EnvReset(GetCurrentEnvironment());
00343 }
00344 #endif
00345
00346
00347
00348
00349
00350 globle void EnvReset(
00351 void *theEnv)
00352 {
00353 struct callFunctionItem *resetPtr;
00354
00355
00356
00357
00358
00359
00360 if (ConstructData(theEnv)->ResetInProgress) return;
00361
00362 ConstructData(theEnv)->ResetInProgress = TRUE;
00363 ConstructData(theEnv)->ResetReadyInProgress = TRUE;
00364
00365
00366
00367
00368
00369
00370 if (EvaluationData(theEnv)->CurrentEvaluationDepth == 0) SetHaltExecution(theEnv,FALSE);
00371
00372
00373
00374
00375
00376
00377
00378
00379 if ((ConstructData(theEnv)->BeforeResetFunction != NULL) ?
00380 ((*ConstructData(theEnv)->BeforeResetFunction)(theEnv) == FALSE) :
00381 FALSE)
00382 {
00383 ConstructData(theEnv)->ResetReadyInProgress = FALSE;
00384 ConstructData(theEnv)->ResetInProgress = FALSE;
00385 return;
00386 }
00387 ConstructData(theEnv)->ResetReadyInProgress = FALSE;
00388
00389
00390
00391
00392
00393 for (resetPtr = ConstructData(theEnv)->ListOfResetFunctions;
00394 (resetPtr != NULL) && (GetHaltExecution(theEnv) == FALSE);
00395 resetPtr = resetPtr->next)
00396 {
00397 if (resetPtr->environmentAware)
00398 { (*resetPtr->func)(theEnv); }
00399 else
00400 { (* (void (*)(void)) resetPtr->func)(); }
00401 }
00402
00403
00404
00405
00406
00407 EnvSetCurrentModule(theEnv,(void *) EnvFindDefmodule(theEnv,"MAIN"));
00408
00409
00410
00411
00412
00413
00414 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00415 (EvaluationData(theEnv)->CurrentExpression == NULL))
00416 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00417
00418
00419
00420
00421
00422 ConstructData(theEnv)->ResetInProgress = FALSE;
00423 }
00424
00425
00426
00427
00428
00429 globle int (*SetBeforeResetFunction(void *theEnv,
00430 int (*theFunction)(void *)))(void *)
00431 {
00432 int (*tempFunction)(void *);
00433
00434 tempFunction = ConstructData(theEnv)->BeforeResetFunction;
00435 ConstructData(theEnv)->BeforeResetFunction = theFunction;
00436 return(tempFunction);
00437 }
00438
00439 #if ALLOW_ENVIRONMENT_GLOBALS
00440
00441
00442
00443
00444 globle intBool AddResetFunction(
00445 char *name,
00446 void (*functionPtr)(void),
00447 int priority)
00448 {
00449 void *theEnv;
00450
00451 theEnv = GetCurrentEnvironment();
00452
00453 ConstructData(theEnv)->ListOfResetFunctions =
00454 AddFunctionToCallList(theEnv,name,priority,(void (*)(void *)) functionPtr,
00455 ConstructData(theEnv)->ListOfResetFunctions,FALSE);
00456 return(TRUE);
00457 }
00458 #endif
00459
00460
00461
00462
00463
00464 globle intBool EnvAddResetFunction(
00465 void *theEnv,
00466 char *name,
00467 void (*functionPtr)(void *),
00468 int priority)
00469 {
00470 ConstructData(theEnv)->ListOfResetFunctions = AddFunctionToCallList(theEnv,name,priority,
00471 functionPtr,
00472 ConstructData(theEnv)->ListOfResetFunctions,TRUE);
00473 return(TRUE);
00474 }
00475
00476
00477
00478
00479
00480 globle intBool EnvRemoveResetFunction(
00481 void *theEnv,
00482 char *name)
00483 {
00484 int found;
00485
00486 ConstructData(theEnv)->ListOfResetFunctions =
00487 RemoveFunctionFromCallList(theEnv,name,ConstructData(theEnv)->ListOfResetFunctions,&found);
00488
00489 if (found) return(TRUE);
00490
00491 return(FALSE);
00492 }
00493
00494
00495
00496
00497
00498 #if ALLOW_ENVIRONMENT_GLOBALS
00499 globle void Clear()
00500 {
00501 EnvClear(GetCurrentEnvironment());
00502 }
00503 #endif
00504
00505
00506
00507
00508 globle void EnvClear(
00509 void *theEnv)
00510 {
00511 struct callFunctionItem *theFunction;
00512
00513
00514
00515
00516
00517
00518
00519 #if DEBUGGING_FUNCTIONS
00520 EnvActivateRouter(theEnv,WTRACE);
00521 #endif
00522
00523
00524
00525
00526
00527 ConstructData(theEnv)->ClearReadyInProgress = TRUE;
00528 if (ClearReady(theEnv) == FALSE)
00529 {
00530 PrintErrorID(theEnv,"CONSTRCT",1,FALSE);
00531 EnvPrintRouter(theEnv,WERROR,"Some constructs are still in use. Clear cannot continue.\n");
00532 #if DEBUGGING_FUNCTIONS
00533 EnvDeactivateRouter(theEnv,WTRACE);
00534 #endif
00535 ConstructData(theEnv)->ClearReadyInProgress = FALSE;
00536 return;
00537 }
00538 ConstructData(theEnv)->ClearReadyInProgress = FALSE;
00539
00540
00541
00542
00543
00544 ConstructData(theEnv)->ClearInProgress = TRUE;
00545
00546 for (theFunction = ConstructData(theEnv)->ListOfClearFunctions;
00547 theFunction != NULL;
00548 theFunction = theFunction->next)
00549 {
00550 if (theFunction->environmentAware)
00551 { (*theFunction->func)(theEnv); }
00552 else
00553 { (* (void (*)(void)) theFunction->func)(); }
00554 }
00555
00556
00557
00558
00559
00560
00561 #if DEBUGGING_FUNCTIONS
00562 EnvDeactivateRouter(theEnv,WTRACE);
00563 #endif
00564
00565
00566
00567
00568
00569
00570 if ((EvaluationData(theEnv)->CurrentEvaluationDepth == 0) && (! CommandLineData(theEnv)->EvaluatingTopLevelCommand) &&
00571 (EvaluationData(theEnv)->CurrentExpression == NULL))
00572 { PeriodicCleanup(theEnv,TRUE,FALSE); }
00573
00574
00575
00576
00577
00578 ConstructData(theEnv)->ClearInProgress = FALSE;
00579
00580 if ((DefruleData(theEnv)->RightPrimeJoins != NULL) ||
00581 (DefruleData(theEnv)->LeftPrimeJoins != NULL))
00582 { SystemError(theEnv,"CONSTRCT",1); }
00583
00584
00585
00586
00587
00588 EnvReset(theEnv);
00589 }
00590
00591
00592
00593
00594
00595
00596
00597 globle intBool ClearReady(
00598 void *theEnv)
00599 {
00600 struct callFunctionItem *theFunction;
00601 int (*tempFunction)(void *);
00602
00603 for (theFunction = ConstructData(theEnv)->ListOfClearReadyFunctions;
00604 theFunction != NULL;
00605 theFunction = theFunction->next)
00606 {
00607 tempFunction = (int (*)(void *)) theFunction->func;
00608 if ((*tempFunction)(theEnv) == FALSE)
00609 { return(FALSE); }
00610 }
00611
00612 return(TRUE);
00613 }
00614
00615
00616
00617
00618
00619 globle intBool AddClearReadyFunction(
00620 void *theEnv,
00621 char *name,
00622 int (*functionPtr)(void *),
00623 int priority)
00624 {
00625 ConstructData(theEnv)->ListOfClearReadyFunctions =
00626 AddFunctionToCallList(theEnv,name,priority,
00627 (void (*)(void *)) functionPtr,
00628 ConstructData(theEnv)->ListOfClearReadyFunctions,TRUE);
00629 return(1);
00630 }
00631
00632
00633
00634
00635
00636 globle intBool RemoveClearReadyFunction(
00637 void *theEnv,
00638 char *name)
00639 {
00640 int found;
00641
00642 ConstructData(theEnv)->ListOfClearReadyFunctions =
00643 RemoveFunctionFromCallList(theEnv,name,ConstructData(theEnv)->ListOfClearReadyFunctions,&found);
00644
00645 if (found) return(TRUE);
00646
00647 return(FALSE);
00648 }
00649
00650 #if ALLOW_ENVIRONMENT_GLOBALS
00651
00652
00653
00654
00655 globle intBool AddClearFunction(
00656 char *name,
00657 void (*functionPtr)(void),
00658 int priority)
00659 {
00660 void *theEnv;
00661
00662 theEnv = GetCurrentEnvironment();
00663
00664 ConstructData(theEnv)->ListOfClearFunctions =
00665 AddFunctionToCallList(theEnv,name,priority,
00666 (void (*)(void *)) functionPtr,
00667 ConstructData(theEnv)->ListOfClearFunctions,FALSE);
00668 return(1);
00669 }
00670 #endif
00671
00672
00673
00674
00675
00676 globle intBool EnvAddClearFunction(
00677 void *theEnv,
00678 char *name,
00679 void (*functionPtr)(void *),
00680 int priority)
00681 {
00682 ConstructData(theEnv)->ListOfClearFunctions =
00683 AddFunctionToCallList(theEnv,name,priority,
00684 (void (*)(void *)) functionPtr,
00685 ConstructData(theEnv)->ListOfClearFunctions,TRUE);
00686 return(1);
00687 }
00688
00689
00690
00691
00692
00693 globle intBool EnvRemoveClearFunction(
00694 void *theEnv,
00695 char *name)
00696 {
00697 int found;
00698
00699 ConstructData(theEnv)->ListOfClearFunctions =
00700 RemoveFunctionFromCallList(theEnv,name,ConstructData(theEnv)->ListOfClearFunctions,&found);
00701
00702 if (found) return(TRUE);
00703
00704 return(FALSE);
00705 }
00706
00707
00708
00709
00710
00711
00712 globle int ExecutingConstruct(
00713 void *theEnv)
00714 {
00715 return(ConstructData(theEnv)->Executing);
00716 }
00717
00718
00719
00720
00721
00722
00723
00724 globle void SetExecutingConstruct(
00725 void *theEnv,
00726 int value)
00727 {
00728 ConstructData(theEnv)->Executing = value;
00729 }
00730
00731
00732
00733
00734
00735
00736
00737 globle void OldGetConstructList(
00738 void *theEnv,
00739 DATA_OBJECT_PTR returnValue,
00740 void *(*nextFunction)(void *,void *),
00741 char *(*nameFunction)(void *,void *))
00742 {
00743 void *theConstruct;
00744 unsigned long count = 0;
00745 struct multifield *theList;
00746
00747
00748
00749
00750
00751
00752 for (theConstruct = (*nextFunction)(theEnv,NULL);
00753 theConstruct != NULL;
00754 theConstruct = (*nextFunction)(theEnv,theConstruct))
00755 { count++; }
00756
00757
00758
00759
00760
00761
00762 SetpType(returnValue,MULTIFIELD);
00763 SetpDOBegin(returnValue,1);
00764 SetpDOEnd(returnValue,(long) count);
00765 theList = (struct multifield *) EnvCreateMultifield(theEnv,count);
00766 SetpValue(returnValue,(void *) theList);
00767
00768
00769
00770
00771
00772 for (theConstruct = (*nextFunction)(theEnv,NULL), count = 1;
00773 theConstruct != NULL;
00774 theConstruct = (*nextFunction)(theEnv,theConstruct), count++)
00775 {
00776 if (EvaluationData(theEnv)->HaltExecution == TRUE)
00777 {
00778 EnvSetMultifieldErrorValue(theEnv,returnValue);
00779 return;
00780 }
00781 SetMFType(theList,count,SYMBOL);
00782 SetMFValue(theList,count,EnvAddSymbol(theEnv,(*nameFunction)(theEnv,theConstruct)));
00783 }
00784 }
00785
00786
00787
00788
00789
00790
00791
00792 globle void DeinstallConstructHeader(
00793 void *theEnv,
00794 struct constructHeader *theHeader)
00795 {
00796 DecrementSymbolCount(theEnv,theHeader->name);
00797 if (theHeader->ppForm != NULL)
00798 {
00799 rm(theEnv,theHeader->ppForm,
00800 sizeof(char) * (strlen(theHeader->ppForm) + 1));
00801 theHeader->ppForm = NULL;
00802 }
00803
00804 if (theHeader->usrData != NULL)
00805 {
00806 ClearUserDataList(theEnv,theHeader->usrData);
00807 theHeader->usrData = NULL;
00808 }
00809 }
00810
00811
00812
00813
00814
00815
00816
00817 globle void DestroyConstructHeader(
00818 void *theEnv,
00819 struct constructHeader *theHeader)
00820 {
00821 if (theHeader->ppForm != NULL)
00822 {
00823 rm(theEnv,theHeader->ppForm,
00824 sizeof(char) * (strlen(theHeader->ppForm) + 1));
00825 theHeader->ppForm = NULL;
00826 }
00827
00828 if (theHeader->usrData != NULL)
00829 {
00830 ClearUserDataList(theEnv,theHeader->usrData);
00831 theHeader->usrData = NULL;
00832 }
00833 }
00834
00835
00836
00837
00838
00839 globle struct construct *AddConstruct(
00840 void *theEnv,
00841 char *name,
00842 char *pluralName,
00843 int (*parseFunction)(void *,char *),
00844 void *(*findFunction)(void *,char *),
00845 SYMBOL_HN *(*getConstructNameFunction)(struct constructHeader *),
00846 char *(*getPPFormFunction)(void *,struct constructHeader *),
00847 struct defmoduleItemHeader *(*getModuleItemFunction)(struct constructHeader *),
00848 void *(*getNextItemFunction)(void *,void *),
00849 void (*setNextItemFunction)(struct constructHeader *,struct constructHeader *),
00850 intBool (*isConstructDeletableFunction)(void *,void *),
00851 int (*deleteFunction)(void *,void *),
00852 void (*freeFunction)(void *,void *))
00853 {
00854 struct construct *newPtr;
00855
00856
00857
00858
00859
00860
00861 newPtr = get_struct(theEnv,construct);
00862
00863 newPtr->constructName = name;
00864 newPtr->pluralName = pluralName;
00865 newPtr->parseFunction = parseFunction;
00866 newPtr->findFunction = findFunction;
00867 newPtr->getConstructNameFunction = getConstructNameFunction;
00868 newPtr->getPPFormFunction = getPPFormFunction;
00869 newPtr->getModuleItemFunction = getModuleItemFunction;
00870 newPtr->getNextItemFunction = getNextItemFunction;
00871 newPtr->setNextItemFunction = setNextItemFunction;
00872 newPtr->isConstructDeletableFunction = isConstructDeletableFunction;
00873 newPtr->deleteFunction = deleteFunction;
00874 newPtr->freeFunction = freeFunction;
00875
00876
00877
00878
00879
00880
00881 newPtr->next = ConstructData(theEnv)->ListOfConstructs;
00882 ConstructData(theEnv)->ListOfConstructs = newPtr;
00883 return(newPtr);
00884 }
00885
00886
00887
00888
00889
00890 globle intBool AddSaveFunction(
00891 void *theEnv,
00892 char *name,
00893 void (*functionPtr)(void *,void *,char *),
00894 int priority)
00895 {
00896 #if (MAC_MCW || WIN_MCW) && (RUN_TIME || BLOAD_ONLY)
00897 #pragma unused(name)
00898 #pragma unused(functionPtr)
00899 #pragma unused(priority)
00900 #endif
00901
00902 #if (! RUN_TIME) && (! BLOAD_ONLY)
00903 ConstructData(theEnv)->ListOfSaveFunctions =
00904 AddFunctionToCallList(theEnv,name,priority,
00905 (void (*)(void *)) functionPtr,
00906 ConstructData(theEnv)->ListOfSaveFunctions,TRUE);
00907 #else
00908 #if MAC_MCW || WIN_MCW || MAC_XCD
00909 #pragma unused(theEnv)
00910 #endif
00911 #endif
00912
00913 return(1);
00914 }