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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 03/18/19 */
/* */
/* DEFMODULE UTILITY MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides routines for parsing module/construct */
/* names and searching through modules for specific */
/* constructs. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.30: Used genstrncpy instead of strncpy. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* 6.31: Used strstr function to find module separator. */
/* */
/* Disallowed use of extraneous module */
/* specifiers in a construct name. */
/* */
/* 6.40: Added Env prefix to GetHaltExecution and */
/* SetHaltExecution functions. */
/* */
/* Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/*************************************************************/
#include "setup.h"
#include "envrnmnt.h"
#include "cstrcpsr.h"
#include "memalloc.h"
#include "modulpsr.h"
#include "pprint.h"
#include "prntutil.h"
#include "router.h"
#include "sysdep.h"
#include "watch.h"
#include "modulutl.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static ConstructHeader *SearchImportedConstructModules(Environment *,CLIPSLexeme *,Defmodule *,
struct moduleItem *,CLIPSLexeme *,
unsigned int *,bool,Defmodule *);
/********************************************************************/
/* FindModuleSeparator: Finds the :: separator which delineates the */
/* boundary between a module name and a construct name. The value */
/* zero is returned if the separator is not found, otherwise the */
/* position of the second colon within the string is returned. */
/********************************************************************/
unsigned FindModuleSeparator(
const char *theString)
{
const char *sep;
sep = strstr(theString,"::");
if (sep == NULL)
{ return 0; }
return ((unsigned) (sep - theString) + 1);
}
/*******************************************************************/
/* ExtractModuleName: Given the position of the :: separator and a */
/* module/construct name joined using the separator, returns a */
/* symbol reference to the module name (or NULL if a module name */
/* cannot be extracted). */
/*******************************************************************/
CLIPSLexeme *ExtractModuleName(
Environment *theEnv,
unsigned thePosition,
const char *theString)
{
char *newString;
CLIPSLexeme *returnValue;
/*=============================================*/
/* Return NULL if the :: is in a position such */
/* that a module name can't be extracted. */
/*=============================================*/
if (thePosition <= 1) return NULL;
/*==========================================*/
/* Allocate storage for a temporary string. */
/*==========================================*/
newString = (char *) gm2(theEnv,thePosition);
/*======================================================*/
/* Copy the entire module/construct name to the string. */
/*======================================================*/
genstrncpy(newString,theString,
(STD_SIZE) thePosition - 1);
/*========================================================*/
/* Place an end of string marker where the :: is located. */
/*========================================================*/
newString[thePosition-1] = EOS;
/*=====================================================*/
/* Add the module name (the truncated module/construct */
/* name) to the symbol table. */
/*=====================================================*/
returnValue = CreateSymbol(theEnv,newString);
/*=============================================*/
/* Return the storage of the temporary string. */
/*=============================================*/
rm(theEnv,newString,thePosition);
/*=============================================*/
/* Return a pointer to the module name symbol. */
/*=============================================*/
return returnValue;
}
/********************************************************************/
/* ExtractConstructName: Given the position of the :: separator and */
/* a module/construct name joined using the separator, returns a */
/* symbol reference to the construct name (or NULL if a construct */
/* name cannot be extracted). */
/********************************************************************/
CLIPSLexeme *ExtractConstructName(
Environment *theEnv,
unsigned thePosition,
const char *theString,
unsigned returnType)
{
size_t theLength;
char *newString;
CLIPSLexeme *returnValue;
/*======================================*/
/* Just return the string if it doesn't */
/* contain the :: symbol. */
/*======================================*/
if (thePosition == 0) return CreateSymbol(theEnv,theString);
/*=====================================*/
/* Determine the length of the string. */
/*=====================================*/
theLength = strlen(theString);
/*=================================================*/
/* Return NULL if the :: is at the very end of the */
/* string (and thus there is no construct name). */
/*=================================================*/
if (theLength <= (thePosition + 1)) return NULL;
/*====================================*/
/* Allocate a temporary string large */
/* enough to hold the construct name. */
/*====================================*/
newString = (char *) gm2(theEnv,theLength - thePosition);
/*================================================*/
/* Copy the construct name portion of the */
/* module/construct name to the temporary string. */
/*================================================*/
genstrncpy(newString,&theString[thePosition+1],
(STD_SIZE) theLength - thePosition);
/*=============================================*/
/* Add the construct name to the symbol table. */
/*=============================================*/
if (returnType == SYMBOL_TYPE)
{ returnValue = CreateSymbol(theEnv,newString); }
else if (returnType == INSTANCE_NAME_TYPE)
{ returnValue = CreateInstanceName(theEnv,newString); }
else
{ returnValue = CreateString(theEnv,newString); }
/*=============================================*/
/* Return the storage of the temporary string. */
/*=============================================*/
rm(theEnv,newString,theLength - thePosition);
/*================================================*/
/* Return a pointer to the construct name symbol. */
/*================================================*/
return returnValue;
}
/****************************************************/
/* ExtractModuleAndConstructName: Extracts both the */
/* module and construct name from a string. Sets */
/* the current module to the specified module. */
/****************************************************/
const char *ExtractModuleAndConstructName(
Environment *theEnv,
const char *theName)
{
unsigned separatorPosition;
CLIPSLexeme *moduleName, *shortName;
Defmodule *theModule;
/*========================*/
/* Find the :: separator. */
/*========================*/
separatorPosition = FindModuleSeparator(theName);
if (! separatorPosition) return(theName);
/*==========================*/
/* Extract the module name. */
/*==========================*/
moduleName = ExtractModuleName(theEnv,separatorPosition,theName);
if (moduleName == NULL) return NULL;
/*====================================*/
/* Check to see if the module exists. */
/*====================================*/
theModule = FindDefmodule(theEnv,moduleName->contents);
if (theModule == NULL) return NULL;
/*============================*/
/* Change the current module. */
/*============================*/
SetCurrentModule(theEnv,theModule);
/*=============================*/
/* Extract the construct name. */
/*=============================*/
shortName = ExtractConstructName(theEnv,separatorPosition,theName,SYMBOL_TYPE);
if (shortName == NULL) return NULL;
return shortName->contents;
}
/************************************************************/
/* FindImportedConstruct: High level routine which searches */
/* a module and other modules from which it imports */
/* constructs for a specified construct. */
/************************************************************/
ConstructHeader *FindImportedConstruct(
Environment *theEnv,
const char *constructName,
Defmodule *matchModule,
const char *findName,
unsigned int *count,
bool searchCurrent,
Defmodule *notYetDefinedInModule)
{
ConstructHeader *rv;
struct moduleItem *theModuleItem;
/*=============================================*/
/* Set the number of references found to zero. */
/*=============================================*/
*count = 0;
/*===============================*/
/* The :: should not be included */
/* in the construct's name. */
/*===============================*/
if (FindModuleSeparator(findName)) return NULL;
/*=============================================*/
/* Remember the current module since we'll be */
/* changing it during the search and will want */
/* to restore it once the search is completed. */
/*=============================================*/
SaveCurrentModule(theEnv);
/*==========================================*/
/* Find the module related access functions */
/* for the construct type being sought. */
/*==========================================*/
if ((theModuleItem = FindModuleItem(theEnv,constructName)) == NULL)
{
RestoreCurrentModule(theEnv);
return NULL;
}
/*===========================================*/
/* If the construct type doesn't have a find */
/* function, then we can't look for it. */
/*===========================================*/
if (theModuleItem->findFunction == NULL)
{
RestoreCurrentModule(theEnv);
return NULL;
}
/*==================================*/
/* Initialize the search by marking */
/* all modules as unvisited. */
/*==================================*/
MarkModulesAsUnvisited(theEnv);
/*===========================*/
/* Search for the construct. */
/*===========================*/
rv = SearchImportedConstructModules(theEnv,CreateSymbol(theEnv,constructName),
matchModule,theModuleItem,
CreateSymbol(theEnv,findName),count,
searchCurrent,notYetDefinedInModule);
/*=============================*/
/* Restore the current module. */
/*=============================*/
RestoreCurrentModule(theEnv);
/*====================================*/
/* Return a pointer to the construct. */
/*====================================*/
return rv;
}
/*********************************************************/
/* AmbiguousReferenceErrorMessage: Error message printed */
/* when a reference to a specific construct can be */
/* imported from more than one module. */
/*********************************************************/
void AmbiguousReferenceErrorMessage(
Environment *theEnv,
const char *constructName,
const char *findName)
{
WriteString(theEnv,STDERR,"Ambiguous reference to ");
WriteString(theEnv,STDERR,constructName);
WriteString(theEnv,STDERR," ");
WriteString(theEnv,STDERR,findName);
WriteString(theEnv,STDERR,".\nIt is imported from more than one module.\n");
}
/****************************************************/
/* MarkModulesAsUnvisited: Used for initializing a */
/* search through the module heirarchies. Sets */
/* the visited flag of each module to false. */
/****************************************************/
void MarkModulesAsUnvisited(
Environment *theEnv)
{
Defmodule *theModule;
DefmoduleData(theEnv)->CurrentModule->visitedFlag = false;
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{ theModule->visitedFlag = false; }
}
/***********************************************************/
/* SearchImportedConstructModules: Low level routine which */
/* searches a module and other modules from which it */
/* imports constructs for a specified construct. */
/***********************************************************/
static ConstructHeader *SearchImportedConstructModules(
Environment *theEnv,
CLIPSLexeme *constructType,
Defmodule *matchModule,
struct moduleItem *theModuleItem,
CLIPSLexeme *findName,
unsigned int *count,
bool searchCurrent,
Defmodule *notYetDefinedInModule)
{
Defmodule *theModule;
struct portItem *theImportList, *theExportList;
ConstructHeader *rv, *arv = NULL;
bool searchModule, exported;
Defmodule *currentModule;
/*=========================================*/
/* Start the search in the current module. */
/* If the current module has already been */
/* visited, then return. */
/*=========================================*/
currentModule = GetCurrentModule(theEnv);
if (currentModule->visitedFlag) return NULL;
/*=======================================================*/
/* The searchCurrent flag indicates whether the current */
/* module should be included in the search. In addition, */
/* if matchModule is non-NULL, the current module will */
/* only be searched if it is the specific module from */
/* which we want the construct imported. */
/*=======================================================*/
if ((searchCurrent) &&
((matchModule == NULL) || (currentModule == matchModule)))
{
/*===============================================*/
/* Look for the construct in the current module. */
/*===============================================*/
rv = (*theModuleItem->findFunction)(theEnv,findName->contents);
/*========================================================*/
/* If we're in the process of defining the construct in */
/* the module we're searching then go ahead and increment */
/* the count indicating the number of modules in which */
/* the construct was found. */
/*========================================================*/
if (notYetDefinedInModule == currentModule)
{
(*count)++;
arv = rv;
}
/*=========================================================*/
/* Otherwise, if the construct is in the specified module, */
/* increment the count only if the construct actually */
/* belongs to the module. [Some constructs, like the COOL */
/* system classes, can be found in any module, but they */
/* actually belong to the MAIN module.] */
/*=========================================================*/
else if (rv != NULL)
{
if (rv->whichModule->theModule == currentModule)
{ (*count)++; }
arv = rv;
}
}
/*=====================================*/
/* Mark the current module as visited. */
/*=====================================*/
currentModule->visitedFlag = true;
/*===================================*/
/* Search through all of the modules */
/* imported by the current module. */
/*===================================*/
theModule = GetCurrentModule(theEnv);
theImportList = theModule->importList;
while (theImportList != NULL)
{
/*===================================================*/
/* Determine if the module should be searched (based */
/* upon whether the entire module, all constructs of */
/* a specific type, or specifically named constructs */
/* are imported). */
/*===================================================*/
searchModule = false;
if ((theImportList->constructType == NULL) ||
(theImportList->constructType == constructType))
{
if ((theImportList->constructName == NULL) ||
(theImportList->constructName == findName))
{ searchModule = true; }
}
/*=================================*/
/* Determine if the module exists. */
/*=================================*/
if (searchModule)
{
theModule = FindDefmodule(theEnv,theImportList->moduleName->contents);
if (theModule == NULL) searchModule = false;
}
/*=======================================================*/
/* Determine if the construct is exported by the module. */
/*=======================================================*/
if (searchModule)
{
exported = false;
theExportList = theModule->exportList;
while ((theExportList != NULL) && (! exported))
{
if ((theExportList->constructType == NULL) ||
(theExportList->constructType == constructType))
{
if ((theExportList->constructName == NULL) ||
(theExportList->constructName == findName))
{ exported = true; }
}
theExportList = theExportList->next;
}
if (! exported) searchModule = false;
}
/*=================================*/
/* Search in the specified module. */
/*=================================*/
if (searchModule)
{
SetCurrentModule(theEnv,theModule);
if ((rv = SearchImportedConstructModules(theEnv,constructType,matchModule,
theModuleItem,findName,
count,true,
notYetDefinedInModule)) != NULL)
{ arv = rv; }
}
/*====================================*/
/* Move on to the next imported item. */
/*====================================*/
theImportList = theImportList->next;
}
/*=========================*/
/* Return a pointer to the */
/* last construct found. */
/*=========================*/
return arv;
}
/**************************************************************/
/* ConstructExported: Returns true if the specified construct */
/* is exported from the specified module. */
/**************************************************************/
bool ConstructExported(
Environment *theEnv,
const char *constructTypeStr,
CLIPSLexeme *moduleName,
CLIPSLexeme *findName)
{
CLIPSLexeme *constructType;
Defmodule *theModule;
struct portItem *theExportList;
constructType = FindSymbolHN(theEnv,constructTypeStr,SYMBOL_BIT);
theModule = FindDefmodule(theEnv,moduleName->contents);
if ((constructType == NULL) || (theModule == NULL) || (findName == NULL))
{ return false; }
theExportList = theModule->exportList;
while (theExportList != NULL)
{
if ((theExportList->constructType == NULL) ||
(theExportList->constructType == constructType))
{
if ((theExportList->constructName == NULL) ||
(theExportList->constructName == findName))
{ return true; }
}
theExportList = theExportList->next;
}
return false;
}
/*********************************************************/
/* AllImportedModulesVisited: Returns true if all of the */
/* imported modules for a module have been visited. */
/*********************************************************/
bool AllImportedModulesVisited(
Environment *theEnv,
Defmodule *theModule)
{
struct portItem *theImportList;
Defmodule *theImportModule;
theImportList = theModule->importList;
while (theImportList != NULL)
{
theImportModule = FindDefmodule(theEnv,theImportList->moduleName->contents);
if (! theImportModule->visitedFlag) return false;
theImportList = theImportList->next;
}
return true;
}
/***************************************/
/* ListItemsDriver: Driver routine for */
/* listing items in a module. */
/***************************************/
void ListItemsDriver(
Environment *theEnv,
const char *logicalName,
Defmodule *theModule,
const char *singleName,
const char *pluralName,
GetNextItemFunction *nextFunction,
const char *(*nameFunction)(void *),
PrintItemFunction *printFunction,
bool (*doItFunction)(void *))
{
void *constructPtr;
const char *constructName;
unsigned long count = 0;
bool allModules = false;
bool doIt;
/*==========================*/
/* Save the current module. */
/*==========================*/
SaveCurrentModule(theEnv);
/*======================*/
/* Print out the items. */
/*======================*/
if (theModule == NULL)
{
theModule = GetNextDefmodule(theEnv,NULL);
allModules = true;
}
while (theModule != NULL)
{
if (allModules)
{
WriteString(theEnv,logicalName,DefmoduleName(theModule));
WriteString(theEnv,logicalName,":\n");
}
SetCurrentModule(theEnv,theModule);
constructPtr = (*nextFunction)(theEnv,NULL);
while (constructPtr != NULL)
{
if (EvaluationData(theEnv)->HaltExecution == true) return;
if (doItFunction == NULL) doIt = true;
else doIt = (*doItFunction)(constructPtr);
if (! doIt) {}
else if (nameFunction != NULL)
{
constructName = (*nameFunction)(constructPtr);
if (constructName != NULL)
{
if (allModules) WriteString(theEnv,logicalName," ");
WriteString(theEnv,logicalName,constructName);
WriteString(theEnv,logicalName,"\n");
}
}
else if (printFunction != NULL)
{
if (allModules) WriteString(theEnv,logicalName," ");
(*printFunction)(theEnv,logicalName,constructPtr);
WriteString(theEnv,logicalName,"\n");
}
constructPtr = (*nextFunction)(theEnv,constructPtr);
count++;
}
if (allModules) theModule = GetNextDefmodule(theEnv,theModule);
else theModule = NULL;
}
/*=================================================*/
/* Print the tally and restore the current module. */
/*=================================================*/
if (singleName != NULL) PrintTally(theEnv,logicalName,count,singleName,pluralName);
RestoreCurrentModule(theEnv);
}
/********************************************************/
/* DoForAllModules: Executes an action for all modules. */
/********************************************************/
long DoForAllModules(
Environment *theEnv,
void (*actionFunction)(Defmodule *,void *),
int interruptable,
void *userBuffer)
{
Defmodule *theModule;
long moduleCount = 0L;
/*==========================*/
/* Save the current module. */
/*==========================*/
SaveCurrentModule(theEnv);
/*==================================*/
/* Loop through all of the modules. */
/*==================================*/
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule), moduleCount++)
{
SetCurrentModule(theEnv,theModule);
if ((interruptable) && GetHaltExecution(theEnv))
{
RestoreCurrentModule(theEnv);
return(-1L);
}
(*actionFunction)(theModule,userBuffer);
}
/*=============================*/
/* Restore the current module. */
/*=============================*/
RestoreCurrentModule(theEnv);
/*=========================================*/
/* Return the number of modules traversed. */
/*=========================================*/
return(moduleCount);
}
#if (! RUN_TIME) && (! BLOAD_ONLY)
/****************************************/
/* RemoveConstructFromModule: Removes a */
/* construct from its module's list */
/****************************************/
void RemoveConstructFromModule(
Environment *theEnv,
ConstructHeader *theConstruct)
{
ConstructHeader *lastConstruct,*currentConstruct;
/*==============================*/
/* Find the specified construct */
/* in the module's list. */
/*==============================*/
lastConstruct = NULL;
currentConstruct = theConstruct->whichModule->firstItem;
while (currentConstruct != theConstruct)
{
lastConstruct = currentConstruct;
currentConstruct = currentConstruct->next;
}
/*========================================*/
/* If it wasn't there, something's wrong. */
/*========================================*/
if (currentConstruct == NULL)
{
SystemError(theEnv,"CSTRCPSR",1);
ExitRouter(theEnv,EXIT_FAILURE);
}
/*==========================*/
/* Remove it from the list. */
/*==========================*/
if (lastConstruct == NULL)
{ theConstruct->whichModule->firstItem = theConstruct->next; }
else
{ lastConstruct->next = theConstruct->next; }
/*=================================================*/
/* Update the pointer to the last item in the list */
/* if the construct just deleted was at the end. */
/*=================================================*/
if (theConstruct == theConstruct->whichModule->lastItem)
{ theConstruct->whichModule->lastItem = lastConstruct; }
}
/*********************************************************/
/* GetConstructNameAndComment: Get the name and comment */
/* field of a construct. Returns name of the construct */
/* if no errors are detected, otherwise returns NULL. */
/*********************************************************/
CLIPSLexeme *GetConstructNameAndComment(
Environment *theEnv,
const char *readSource,
struct token *inputToken,
const char *constructName,
FindConstructFunction *findFunction,
DeleteConstructFunction *deleteFunction,
const char *constructSymbol,
bool fullMessageCR,
bool getComment,
bool moduleNameAllowed,
bool ignoreRedefinition)
{
#if (MAC_XCD) && (! DEBUGGING_FUNCTIONS)
#pragma unused(fullMessageCR)
#endif
CLIPSLexeme *name, *moduleName;
bool redefining = false;
ConstructHeader *theConstruct;
unsigned separatorPosition;
Defmodule *theModule;
/*==========================*/
/* Next token should be the */
/* name of the construct. */
/*==========================*/
GetToken(theEnv,readSource,inputToken);
if (inputToken->tknType != SYMBOL_TOKEN)
{
PrintErrorID(theEnv,"CSTRCPSR",2,true);
WriteString(theEnv,STDERR,"Missing name for ");
WriteString(theEnv,STDERR,constructName);
WriteString(theEnv,STDERR," construct.\n");
return NULL;
}
name = inputToken->lexemeValue;
/*===============================*/
/* Determine the current module. */
/*===============================*/
separatorPosition = FindModuleSeparator(name->contents);
if (separatorPosition)
{
if (moduleNameAllowed == false)
{
SyntaxErrorMessage(theEnv,"module specifier");
return NULL;
}
moduleName = ExtractModuleName(theEnv,separatorPosition,name->contents);
if (moduleName == NULL)
{
SyntaxErrorMessage(theEnv,"construct name");
return NULL;
}
theModule = FindDefmodule(theEnv,moduleName->contents);
if (theModule == NULL)
{
CantFindItemErrorMessage(theEnv,"defmodule",moduleName->contents,true);
return NULL;
}
SetCurrentModule(theEnv,theModule);
name = ExtractConstructName(theEnv,separatorPosition,name->contents,SYMBOL_TYPE);
if (name == NULL)
{
SyntaxErrorMessage(theEnv,"construct name");
return NULL;
}
if (FindModuleSeparator(name->contents) != 0)
{
SyntaxErrorMessage(theEnv,"module specifier");
return NULL;
}
}
/*=====================================================*/
/* If the module was not specified, record the current */
/* module name as part of the pretty-print form. */
/*=====================================================*/
else
{
theModule = GetCurrentModule(theEnv);
if (moduleNameAllowed)
{
PPBackup(theEnv);
SavePPBuffer(theEnv,DefmoduleName(theModule));
SavePPBuffer(theEnv,"::");
SavePPBuffer(theEnv,name->contents);
}
}
/*==================================================================*/
/* Check for import/export conflicts from the construct definition. */
/*==================================================================*/
#if DEFMODULE_CONSTRUCT
if (FindImportExportConflict(theEnv,constructName,theModule,name->contents))
{
ImportExportConflictMessage(theEnv,constructName,name->contents,NULL,NULL);
return NULL;
}
#endif
/*========================================================*/
/* Remove the construct if it is already in the knowledge */
/* base and we're not just checking syntax. */
/*========================================================*/
if ((findFunction != NULL) && (! ConstructData(theEnv)->CheckSyntaxMode))
{
theConstruct = (*findFunction)(theEnv,name->contents);
if (theConstruct != NULL)
{
redefining = true;
if (deleteFunction != NULL)
{
RetainLexeme(theEnv,name);
if ((*deleteFunction)(theConstruct,theEnv) == false)
{
PrintErrorID(theEnv,"CSTRCPSR",4,true);
WriteString(theEnv,STDERR,"Cannot redefine ");
WriteString(theEnv,STDERR,constructName);
WriteString(theEnv,STDERR," '");
WriteString(theEnv,STDERR,name->contents);
WriteString(theEnv,STDERR,"' while it is in use.\n");
ReleaseLexeme(theEnv,name);
return NULL;
}
ReleaseLexeme(theEnv,name);
}
}
}
/*=============================================*/
/* If compilations are being watched, indicate */
/* that a construct is being compiled. */
/*=============================================*/
#if DEBUGGING_FUNCTIONS
if ((GetWatchItem(theEnv,"compilations") == 1) &&
GetPrintWhileLoading(theEnv) && (! ConstructData(theEnv)->CheckSyntaxMode))
{
const char *outRouter = STDOUT;
if (redefining && (! ignoreRedefinition))
{
outRouter = STDWRN;
PrintWarningID(theEnv,"CSTRCPSR",1,true);
WriteString(theEnv,outRouter,"Redefining ");
}
else WriteString(theEnv,outRouter,"Defining ");
WriteString(theEnv,outRouter,constructName);
WriteString(theEnv,outRouter,": ");
WriteString(theEnv,outRouter,name->contents);
if (fullMessageCR) WriteString(theEnv,outRouter,"\n");
else WriteString(theEnv,outRouter," ");
}
else
#endif
{
if (GetPrintWhileLoading(theEnv) && (! ConstructData(theEnv)->CheckSyntaxMode))
{ WriteString(theEnv,STDOUT,constructSymbol); }
}
/*===============================*/
/* Get the comment if it exists. */
/*===============================*/
GetToken(theEnv,readSource,inputToken);
if ((inputToken->tknType == STRING_TOKEN) && getComment)
{
PPBackup(theEnv);
SavePPBuffer(theEnv," ");
SavePPBuffer(theEnv,inputToken->printForm);
GetToken(theEnv,readSource,inputToken);
if (inputToken->tknType != RIGHT_PARENTHESIS_TOKEN)
{
PPBackup(theEnv);
SavePPBuffer(theEnv,"\n ");
SavePPBuffer(theEnv,inputToken->printForm);
}
}
else if (inputToken->tknType != RIGHT_PARENTHESIS_TOKEN)
{
PPBackup(theEnv);
SavePPBuffer(theEnv,"\n ");
SavePPBuffer(theEnv,inputToken->printForm);
}
/*===================================*/
/* Return the name of the construct. */
/*===================================*/
return(name);
}
#endif /* (! RUN_TIME) && (! BLOAD_ONLY) */