clips-sys 0.4.0

Bindgen generated wrapper for CLIPS (clipsrules.net)
Documentation
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
   /*******************************************************/
   /*      "C" Language Integrated Production System      */
   /*                                                     */
   /*            CLIPS Version 6.40  10/03/19             */
   /*                                                     */
   /*                  CLASS COMMANDS MODULE              */
   /*******************************************************/

/**************************************************************/
/* Purpose: Kernel Interface Commands for Object System       */
/*                                                            */
/* Principal Programmer(s):                                   */
/*      Brian L. Dantes                                       */
/*                                                            */
/* Contributing Programmer(s):                                */
/*                                                            */
/* Revision History:                                          */
/*                                                            */
/*      6.23: Corrected compilation errors for files          */
/*            generated by constructs-to-c. DR0861            */
/*                                                            */
/*            Changed name of variable log to logName         */
/*            because of Unix compiler warnings of shadowed   */
/*            definitions.                                    */
/*                                                            */
/*      6.24: Renamed BOOLEAN macro type to intBool.          */
/*                                                            */
/*            Added pragmas to remove compilation warnings.   */
/*                                                            */
/*      6.30: Borland C (IBM_TBC) and Metrowerks CodeWarrior  */
/*            (MAC_MCW, IBM_MCW) are no longer supported.     */
/*                                                            */
/*            Added const qualifiers to remove C++            */
/*            deprecation warnings.                           */
/*                                                            */
/*            Converted API macros to function calls.         */
/*                                                            */
/*            Changed find construct functionality so that    */
/*            imported modules are search when locating a     */
/*            named construct.                                */
/*                                                            */
/*      6.40: Pragma once and other inclusion changes.       */
/*                                                           */
/*            Added support for booleans with <stdbool.h>.   */
/*                                                           */
/*            Removed use of void pointers for specific      */
/*            data structures.                               */
/*                                                           */
/*            ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/*                                                           */
/*            UDF redesign.                                  */
/*                                                           */
/*            Pretty print functions accept optional logical */
/*            name argument.                                 */
/*                                                           */
/*************************************************************/

/* =========================================
   *****************************************
               EXTERNAL DEFINITIONS
   =========================================
   ***************************************** */

#include <string.h>

#include "setup.h"

#if OBJECT_SYSTEM

#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
#include "bload.h"
#endif

#include "argacces.h"
#include "classfun.h"
#include "classini.h"
#include "envrnmnt.h"
#include "modulutl.h"
#include "msgcom.h"
#include "prntutil.h"
#include "router.h"

#include "classcom.h"

/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/

#if (! BLOAD_ONLY) && (! RUN_TIME) && DEBUGGING_FUNCTIONS
   static void                    SaveDefclass(Environment *,ConstructHeader *,void *);
#endif
   static const char             *GetClassDefaultsModeName(ClassDefaultsMode);

/* =========================================
   *****************************************
          EXTERNALLY VISIBLE FUNCTIONS
   =========================================
   ***************************************** */

/*******************************************************************
  NAME         : FindDefclass
  DESCRIPTION  : Looks up a specified class in the class hash table
                 (Only looks in current or specified module)
  INPUTS       : The name-string of the class (including module)
  RETURNS      : The address of the found class, NULL otherwise
  SIDE EFFECTS : None
  NOTES        : None
 ******************************************************************/
Defclass *FindDefclass( // TBD Needs to look in imported
  Environment *theEnv,
  const char *classAndModuleName)
  {
   CLIPSLexeme *classSymbol = NULL;
   Defclass *cls;
   Defmodule *theModule = NULL;
   const char *className;

   SaveCurrentModule(theEnv);

   className = ExtractModuleAndConstructName(theEnv,classAndModuleName);
   if (className != NULL)
     {
      classSymbol = FindSymbolHN(theEnv,ExtractModuleAndConstructName(theEnv,classAndModuleName),SYMBOL_BIT);
      theModule = GetCurrentModule(theEnv);
     }

   RestoreCurrentModule(theEnv);

   if (classSymbol == NULL)
     { return NULL; }

   cls = DefclassData(theEnv)->ClassTable[HashClass(classSymbol)];
   while (cls != NULL)
     {
      if (cls->header.name == classSymbol)
        {
         if (cls->system || (cls->header.whichModule->theModule == theModule))
           { return cls->installed ? cls : NULL; }
        }
      cls = cls->nxtHash;
     }

   return NULL;
  }

/*******************************************************************
  NAME         : FindDefclassInModule
  DESCRIPTION  : Looks up a specified class in the class hash table
                 (Only looks in current or specified module)
  INPUTS       : The name-string of the class (including module)
  RETURNS      : The address of the found class, NULL otherwise
  SIDE EFFECTS : None
  NOTES        : None
 ******************************************************************/
Defclass *FindDefclassInModule(
  Environment *theEnv,
  const char *classAndModuleName)
  {
   CLIPSLexeme *classSymbol = NULL;
   Defclass *cls;
   Defmodule *theModule = NULL;
   const char *className;

   SaveCurrentModule(theEnv);
   className = ExtractModuleAndConstructName(theEnv,classAndModuleName);
   if (className != NULL)
     {
      classSymbol = FindSymbolHN(theEnv,ExtractModuleAndConstructName(theEnv,classAndModuleName),SYMBOL_BIT);
      theModule = GetCurrentModule(theEnv);
     }
   RestoreCurrentModule(theEnv);

   if (classSymbol == NULL)
     { return NULL; }

   cls = DefclassData(theEnv)->ClassTable[HashClass(classSymbol)];
   while (cls != NULL)
     {
      if (cls->header.name == classSymbol)
        {
         if (cls->system || (cls->header.whichModule->theModule == theModule))
           { return cls->installed ? cls : NULL; }
        }
      cls = cls->nxtHash;
     }

   return NULL;
  }

/***************************************************
  NAME         : LookupDefclassByMdlOrScope
  DESCRIPTION  : Finds a class anywhere (if module
                 is specified) or in current or
                 imported modules
  INPUTS       : The class name
  RETURNS      : The class (NULL if not found)
  SIDE EFFECTS : Error message printed on
                  ambiguous references
  NOTES        : Assumes no two classes of the same
                 name are ever in the same scope
 ***************************************************/
Defclass *LookupDefclassByMdlOrScope(
  Environment *theEnv,
  const char *classAndModuleName)
  {
   Defclass *cls;
   const char *className;
   CLIPSLexeme *classSymbol;
   Defmodule *theModule;

   if (FindModuleSeparator(classAndModuleName) == 0)
     { return LookupDefclassInScope(theEnv,classAndModuleName); }

   SaveCurrentModule(theEnv);
   className = ExtractModuleAndConstructName(theEnv,classAndModuleName);
   theModule = GetCurrentModule(theEnv);
   RestoreCurrentModule(theEnv);

   if (className == NULL)
     { return NULL; }

   if ((classSymbol = FindSymbolHN(theEnv,className,SYMBOL_BIT)) == NULL)
     { return NULL; }

   cls = DefclassData(theEnv)->ClassTable[HashClass(classSymbol)];
   while (cls != NULL)
     {
      if ((cls->header.name == classSymbol) &&
          (cls->header.whichModule->theModule == theModule))
        return(cls->installed ? cls : NULL);
      cls = cls->nxtHash;
     }

   return NULL;
  }

/****************************************************
  NAME         : LookupDefclassInScope
  DESCRIPTION  : Finds a class in current or imported
                   modules (module specifier
                   is not allowed)
  INPUTS       : The class name
  RETURNS      : The class (NULL if not found)
  SIDE EFFECTS : Error message printed on
                  ambiguous references
  NOTES        : Assumes no two classes of the same
                 name are ever in the same scope
 ****************************************************/
Defclass *LookupDefclassInScope(
  Environment *theEnv,
  const char *className)
  {
   Defclass *cls;
   CLIPSLexeme *classSymbol;

   if ((classSymbol = FindSymbolHN(theEnv,className,SYMBOL_BIT)) == NULL)
     { return NULL; }

   cls = DefclassData(theEnv)->ClassTable[HashClass(classSymbol)];
   while (cls != NULL)
     {
      if ((cls->header.name == classSymbol) && DefclassInScope(theEnv,cls,NULL))
        return cls->installed ? cls : NULL;
      cls = cls->nxtHash;
     }

   return NULL;
  }

/******************************************************
  NAME         : LookupDefclassAnywhere
  DESCRIPTION  : Finds a class in specified
                 (or any) module
  INPUTS       : 1) The module (NULL if don't care)
                 2) The class name (module specifier
                    in name not allowed)
  RETURNS      : The class (NULL if not found)
  SIDE EFFECTS : None
  NOTES        : Does *not* generate an error if
                 multiple classes of the same name
                 exist as do the other lookup functions
 ******************************************************/
Defclass *LookupDefclassAnywhere(
  Environment *theEnv,
  Defmodule *theModule,
  const char *className)
  {
   Defclass *cls;
   CLIPSLexeme *classSymbol;

   if ((classSymbol = FindSymbolHN(theEnv,className,SYMBOL_BIT)) == NULL)
     { return NULL; }

   cls = DefclassData(theEnv)->ClassTable[HashClass(classSymbol)];
   while (cls != NULL)
     {
      if ((cls->header.name == classSymbol) &&
          ((theModule == NULL) ||
           (cls->header.whichModule->theModule == theModule)))
        { return cls->installed ? cls : NULL; }
      cls = cls->nxtHash;
     }

   return NULL;
  }

/***************************************************
  NAME         : DefclassInScope
  DESCRIPTION  : Determines if a defclass is in
                 scope of the given module
  INPUTS       : 1) The defclass
                 2) The module (NULL for current
                    module)
  RETURNS      : True if in scope,
                 false otherwise
  SIDE EFFECTS : None
  NOTES        : None
 ***************************************************/
bool DefclassInScope(
  Environment *theEnv,
  Defclass *theDefclass,
  Defmodule *theModule)
  {
#if DEFMODULE_CONSTRUCT
   unsigned long moduleID;
   char *scopeMap;

   scopeMap = (char *) theDefclass->scopeMap->contents;
   if (theModule == NULL)
     { theModule = GetCurrentModule(theEnv); }
   moduleID = theModule->header.bsaveID;

   return TestBitMap(scopeMap,moduleID);
#else
#if MAC_XCD
#pragma unused(theEnv,theDefclass,theModule)
#endif
   return true;
#endif
  }

/***********************************************************
  NAME         : GetNextDefclass
  DESCRIPTION  : Finds first or next defclass
  INPUTS       : The address of the current defclass
  RETURNS      : The address of the next defclass
                   (NULL if none)
  SIDE EFFECTS : None
  NOTES        : If ptr == NULL, the first defclass
                    is returned.
 ***********************************************************/
Defclass *GetNextDefclass(
  Environment *theEnv,
  Defclass *theDefclass)
  {
   return (Defclass *) GetNextConstructItem(theEnv,&theDefclass->header,
                                            DefclassData(theEnv)->DefclassModuleIndex);
  }

/***************************************************
  NAME         : DefclassIsDeletable
  DESCRIPTION  : Determines if a defclass
                   can be deleted
  INPUTS       : Address of the defclass
  RETURNS      : True if deletable,
                 false otherwise
  SIDE EFFECTS : None
  NOTES        : None
 ***************************************************/
bool DefclassIsDeletable(
  Defclass *theDefclass)
  {
  Environment *theEnv = theDefclass->header.env;
  
  if (! ConstructsDeletable(theEnv))
     { return false; }

   if (theDefclass->system == 1)
     { return false; }

#if (! BLOAD_ONLY) && (! RUN_TIME)
   return (IsClassBeingUsed(theDefclass) == false) ? true : false;
#else
   return false;
#endif
  }

/*************************************************************
  NAME         : UndefclassCommand
  DESCRIPTION  : Deletes a class and its subclasses, as
                 well as their associated instances
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS : None
  NOTES        : Syntax : (undefclass <class-name> | *)
 *************************************************************/
void UndefclassCommand(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   UndefconstructCommand(context,"undefclass",DefclassData(theEnv)->DefclassConstruct);
  }

/********************************************************
  NAME         : Undefclass
  DESCRIPTION  : Deletes the named defclass
  INPUTS       : None
  RETURNS      : True if deleted, or false
  SIDE EFFECTS : Defclass and handlers removed
  NOTES        : Interface for AddConstruct()
 ********************************************************/
bool Undefclass(
  Defclass *theDefclass,
  Environment *allEnv)
  {   
#if RUN_TIME || BLOAD_ONLY
   return false;
#else
   Environment *theEnv;
   bool success;
   GCBlock gcb;
   
   if (theDefclass == NULL)
     { theEnv = allEnv; }
   else
     { theEnv = theDefclass->header.env; }

#if BLOAD || BLOAD_AND_BSAVE
   if (Bloaded(theEnv))
     return false;
#endif

   GCBlockStart(theEnv,&gcb);
   if (theDefclass == NULL)
     {
      success = RemoveAllUserClasses(theEnv);
      GCBlockEnd(theEnv,&gcb);
      return success;
     }

   success = DeleteClassUAG(theEnv,theDefclass);
   GCBlockEnd(theEnv,&gcb);
   return success;
#endif
  }


#if DEBUGGING_FUNCTIONS

/*********************************************************
  NAME         : PPDefclassCommand
  DESCRIPTION  : Displays the pretty print form of
                 a class to stdout.
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS : None
  NOTES        : Syntax : (ppdefclass <class-name>)
 *********************************************************/
void PPDefclassCommand(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   PPConstructCommand(context,"ppdefclass",DefclassData(theEnv)->DefclassConstruct,returnValue);
  }

/***************************************************
  NAME         : ListDefclassesCommand
  DESCRIPTION  : Displays all defclass names
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS : Defclass names printed
  NOTES        : H/L Interface
 ***************************************************/
void ListDefclassesCommand(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   ListConstructCommand(context,DefclassData(theEnv)->DefclassConstruct);
  }

/***************************************************
  NAME         : ListDefclasses
  DESCRIPTION  : Displays all defclass names
  INPUTS       : 1) The logical name of the output
                 2) The module
  RETURNS      : Nothing useful
  SIDE EFFECTS : Defclass names printed
  NOTES        : C Interface
 ***************************************************/
void ListDefclasses(
  Environment *theEnv,
  const char *logicalName,
  Defmodule *theModule)
  {
   ListConstruct(theEnv,DefclassData(theEnv)->DefclassConstruct,logicalName,theModule);
  }

/*********************************************************
  NAME         : DefclassGetWatchInstances
  DESCRIPTION  : Determines if deletions/creations of
                 instances of this class will generate
                 trace messages or not
  INPUTS       : A pointer to the class
  RETURNS      : True if a trace is active,
                 false otherwise
  SIDE EFFECTS : None
  NOTES        : None
 *********************************************************/
bool DefclassGetWatchInstances(
  Defclass *theDefclass)
  {
   return theDefclass->traceInstances;
  }

/*********************************************************
  NAME         : DefclassSetWatchInstances
  DESCRIPTION  : Sets the trace to ON/OFF for the
                 creation/deletion of instances
                 of the class
  INPUTS       : 1) true to set the trace on,
                    false to set it off
                 2) A pointer to the class
  RETURNS      : Nothing useful
  SIDE EFFECTS : Watch flag for the class set
  NOTES        : None
 *********************************************************/
void DefclassSetWatchInstances(
  Defclass *theDefclass,
  bool newState)
  {
   if (theDefclass->abstract)
     { return; }

   theDefclass->traceInstances = newState;
  }

/*********************************************************
  NAME         : DefclassGetWatchSlots
  DESCRIPTION  : Determines if changes to slots of
                 instances of this class will generate
                 trace messages or not
  INPUTS       : A pointer to the class
  RETURNS      : True if a trace is active,
                 false otherwise
  SIDE EFFECTS : None
  NOTES        : None
 *********************************************************/
bool DefclassGetWatchSlots(
  Defclass *theDefclass)
  {
   return theDefclass->traceSlots;
  }

/**********************************************************
  NAME         : SetDefclassWatchSlots
  DESCRIPTION  : Sets the trace to ON/OFF for the
                 changes to slots of instances of the class
  INPUTS       : 1) true to set the trace on,
                    false to set it off
                 2) A pointer to the class
  RETURNS      : Nothing useful
  SIDE EFFECTS : Watch flag for the class set
  NOTES        : None
 **********************************************************/
void DefclassSetWatchSlots(
  Defclass *theDefclass,
  bool newState)
  {
   theDefclass->traceSlots = newState;
  }

/******************************************************************
  NAME         : DefclassWatchAccess
  DESCRIPTION  : Parses a list of class names passed by
                 AddWatchItem() and sets the traces accordingly
  INPUTS       : 1) A code indicating which trace flag is to be set
                    0 - Watch instance creation/deletion
                    1 - Watch slot changes to instances
                 2) The value to which to set the trace flags
                 3) A list of expressions containing the names
                    of the classes for which to set traces
  RETURNS      : True if all OK, false otherwise
  SIDE EFFECTS : Watch flags set in specified classes
  NOTES        : Accessory function for AddWatchItem()
 ******************************************************************/
bool DefclassWatchAccess(
  Environment *theEnv,
  int code,
  bool newState,
  Expression *argExprs)
  {
   if (code)
     return(ConstructSetWatchAccess(theEnv,DefclassData(theEnv)->DefclassConstruct,newState,argExprs,
                                    (ConstructGetWatchFunction *) DefclassGetWatchSlots,
                                    (ConstructSetWatchFunction *) DefclassSetWatchSlots));
   else
     return(ConstructSetWatchAccess(theEnv,DefclassData(theEnv)->DefclassConstruct,newState,argExprs,
                                    (ConstructGetWatchFunction *) DefclassGetWatchInstances,
                                    (ConstructSetWatchFunction *) DefclassSetWatchInstances));
  }

/***********************************************************************
  NAME         : DefclassWatchPrint
  DESCRIPTION  : Parses a list of class names passed by
                 AddWatchItem() and displays the traces accordingly
  INPUTS       : 1) The logical name of the output
                 2) A code indicating which trace flag is to be examined
                    0 - Watch instance creation/deletion
                    1 - Watch slot changes to instances
                 3) A list of expressions containing the names
                    of the classes for which to examine traces
  RETURNS      : True if all OK, false otherwise
  SIDE EFFECTS : Watch flags displayed for specified classes
  NOTES        : Accessory function for AddWatchItem()
 ***********************************************************************/
bool DefclassWatchPrint(
  Environment *theEnv,
  const char *logName,
  int code,
  Expression *argExprs)
  {
   if (code)
     return(ConstructPrintWatchAccess(theEnv,DefclassData(theEnv)->DefclassConstruct,logName,argExprs,
                                      (ConstructGetWatchFunction *) DefclassGetWatchSlots,
                                      (ConstructSetWatchFunction *) DefclassSetWatchSlots));
   else
     return(ConstructPrintWatchAccess(theEnv,DefclassData(theEnv)->DefclassConstruct,logName,argExprs,
                                      (ConstructGetWatchFunction *) DefclassGetWatchInstances,
                                      (ConstructSetWatchFunction *) DefclassSetWatchInstances));
  }

#endif /* DEBUGGING_FUNCTIONS */

/*********************************************************
  NAME         : GetDefclassListFunction
  DESCRIPTION  : Groups names of all defclasses into
                   a multifield variable
  INPUTS       : A data object buffer
  RETURNS      : Nothing useful
  SIDE EFFECTS : Multifield set to list of classes
  NOTES        : None
 *********************************************************/
void GetDefclassListFunction(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   GetConstructListFunction(context,returnValue,DefclassData(theEnv)->DefclassConstruct);
  }

/***************************************************************
  NAME         : GetDefclassList
  DESCRIPTION  : Groups all defclass names into
                 a multifield list
  INPUTS       : 1) A data object buffer to hold
                    the multifield result
                 2) The module from which to obtain defclasses
  RETURNS      : Nothing useful
  SIDE EFFECTS : Multifield allocated and filled
  NOTES        : External C access
 ***************************************************************/
void GetDefclassList(
  Environment *theEnv,
  CLIPSValue *returnValue,
  Defmodule *theModule)
  {
   UDFValue result;
   
   GetConstructList(theEnv,&result,DefclassData(theEnv)->DefclassConstruct,theModule);
   NormalizeMultifield(theEnv,&result);
   returnValue->value = result.value;
  }

/*****************************************************
  NAME         : HasSuperclass
  DESCRIPTION  : Determines if class-2 is a superclass
                   of class-1
  INPUTS       : 1) Class-1
                 2) Class-2
  RETURNS      : True if class-2 is a superclass of
                   class-1, false otherwise
  SIDE EFFECTS : None
  NOTES        : None
 *****************************************************/
bool HasSuperclass(
  Defclass *c1,
  Defclass *c2)
  {
   unsigned long i;

   for (i = 1 ; i < c1->allSuperclasses.classCount ; i++)
     if (c1->allSuperclasses.classArray[i] == c2)
       return true;
   return false;
  }

/********************************************************************
  NAME         : CheckClassAndSlot
  DESCRIPTION  : Checks class and slot argument for various functions
  INPUTS       : 1) Name of the calling function
                 2) Buffer for class address
  RETURNS      : Slot symbol, NULL on errors
  SIDE EFFECTS : None
  NOTES        : None
 ********************************************************************/
CLIPSLexeme *CheckClassAndSlot(
   UDFContext *context,
   const char *func,
   Defclass **cls)
  {
   UDFValue theArg;
   Environment *theEnv = context->environment;

   if (! UDFFirstArgument(context,SYMBOL_BIT,&theArg))
     return NULL;

   *cls = LookupDefclassByMdlOrScope(theEnv,theArg.lexemeValue->contents);
   if (*cls == NULL)
     {
      ClassExistError(theEnv,func,theArg.lexemeValue->contents);
      return NULL;
     }

   if (! UDFNextArgument(context,SYMBOL_BIT,&theArg))
     return NULL;

   return theArg.lexemeValue;
  }

#if (! BLOAD_ONLY) && (! RUN_TIME)

/***************************************************
  NAME         : SaveDefclasses
  DESCRIPTION  : Prints pretty print form of
                   defclasses to specified output
  INPUTS       : The  logical name of the output
  RETURNS      : Nothing useful
  SIDE EFFECTS : None
  NOTES        : None
 ***************************************************/
void SaveDefclasses(
  Environment *theEnv,
  Defmodule *theModule,
  const char *logName,
  void *context)
  {
#if DEBUGGING_FUNCTIONS
   DoForAllConstructsInModule(theEnv,theModule,SaveDefclass,
                              DefclassData(theEnv)->DefclassModuleIndex,
                              false,(void *) logName);
#else
#if MAC_XCD
#pragma unused(theEnv,theModule,logName)
#endif
#endif
  }

#endif

/* =========================================
   *****************************************
          INTERNALLY VISIBLE FUNCTIONS
   =========================================
   ***************************************** */

#if (! BLOAD_ONLY) && (! RUN_TIME) && DEBUGGING_FUNCTIONS

/***************************************************
  NAME         : SaveDefclass
  DESCRIPTION  : Writes out the pretty-print forms
                 of a class and all its handlers
  INPUTS       : 1) The class
                 2) The logical name of the output
  RETURNS      : Nothing useful
  SIDE EFFECTS : Class and handlers written
  NOTES        : None
 ***************************************************/
static void SaveDefclass(
  Environment *theEnv,
  ConstructHeader *theConstruct,
  void *userBuffer)
  {
   const char *logName = (const char *) userBuffer;
   Defclass *theDefclass = (Defclass *) theConstruct;
   unsigned hnd;
   const char *ppForm;

   ppForm = DefclassPPForm(theDefclass);
   if (ppForm != NULL)
     {
      WriteString(theEnv,logName,ppForm);
      WriteString(theEnv,logName,"\n");
      hnd = GetNextDefmessageHandler(theDefclass,0);
      while (hnd != 0)
        {
         ppForm = DefmessageHandlerPPForm(theDefclass,hnd);
         if (ppForm != NULL)
           {
            WriteString(theEnv,logName,ppForm);
            WriteString(theEnv,logName,"\n");
           }
         hnd = GetNextDefmessageHandler(theDefclass,hnd);
        }
     }
  }

#endif

/********************************************/
/* SetClassDefaultsMode: Allows the setting */
/*    of the class defaults mode.           */
/********************************************/
ClassDefaultsMode SetClassDefaultsMode(
  Environment *theEnv,
  ClassDefaultsMode value)
  {
   ClassDefaultsMode ov;

   ov = DefclassData(theEnv)->ClassDefaultsModeValue;
   DefclassData(theEnv)->ClassDefaultsModeValue = value;
   return ov;
  }

/****************************************/
/* GetClassDefaultsMode: Returns the    */
/*    value of the class defaults mode. */
/****************************************/
ClassDefaultsMode GetClassDefaultsMode(
  Environment *theEnv)
  {
   return DefclassData(theEnv)->ClassDefaultsModeValue;
  }

/***************************************************/
/* GetClassDefaultsModeCommand: H/L access routine */
/*   for the get-class-defaults-mode command.      */
/***************************************************/
void GetClassDefaultsModeCommand(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   returnValue->lexemeValue = CreateSymbol(theEnv,GetClassDefaultsModeName(GetClassDefaultsMode(theEnv)));
  }

/***************************************************/
/* SetClassDefaultsModeCommand: H/L access routine */
/*   for the set-class-defaults-mode command.      */
/***************************************************/
void SetClassDefaultsModeCommand(
  Environment *theEnv,
  UDFContext *context,
  UDFValue *returnValue)
  {
   UDFValue theArg;
   const char *argument;
   ClassDefaultsMode oldMode;

   oldMode = DefclassData(theEnv)->ClassDefaultsModeValue;

   /*=====================================================*/
   /* Check for the correct number and type of arguments. */
   /*=====================================================*/

   if (! UDFFirstArgument(context,SYMBOL_BIT,&theArg))
     { return; }

   argument = theArg.lexemeValue->contents;

   /*=============================================*/
   /* Set the strategy to the specified strategy. */
   /*=============================================*/

   if (strcmp(argument,"conservation") == 0)
     { SetClassDefaultsMode(theEnv,CONSERVATION_MODE); }
   else if (strcmp(argument,"convenience") == 0)
     { SetClassDefaultsMode(theEnv,CONVENIENCE_MODE); }
   else
     {
      UDFInvalidArgumentMessage(context,"symbol with value conservation or convenience");
      returnValue->lexemeValue = CreateSymbol(theEnv,GetClassDefaultsModeName(GetClassDefaultsMode(theEnv)));
      return;
     }

   /*===================================*/
   /* Return the old value of the mode. */
   /*===================================*/

   returnValue->lexemeValue = CreateSymbol(theEnv,GetClassDefaultsModeName(oldMode));
  }

/*******************************************************************/
/* GetClassDefaultsModeName: Given the integer value corresponding */
/*   to a specified class defaults mode, return a character string */
/*   of the class defaults mode's name.                            */
/*******************************************************************/
static const char *GetClassDefaultsModeName(
  ClassDefaultsMode mode)
  {
   const char *sname;

   switch (mode)
     {
      case CONSERVATION_MODE:
        sname = "conservation";
        break;
      case CONVENIENCE_MODE:
        sname = "convenience";
        break;
      default:
        sname = "unknown";
        break;
     }

   return(sname);
  }

/*#############################*/
/* Additional Access Functions */
/*#############################*/

CLIPSLexeme *GetDefclassNamePointer(
  Defclass *theClass)
  {
   return GetConstructNamePointer(&theClass->header);
  }

void SetNextDefclass(
  Defclass *theClass,
  Defclass *targetClass)
  {
   SetNextConstruct(&theClass->header,
                    &targetClass->header);
  }

/*##################################*/
/* Additional Environment Functions */
/*##################################*/

const char *DefclassName(
  Defclass *theClass)
  {
   return GetConstructNameString(&theClass->header);
  }

const char *DefclassPPForm(
  Defclass *theClass)
  {
   return GetConstructPPForm(&theClass->header);
  }

struct defmoduleItemHeader *GetDefclassModule(
  Environment *theEnv,
  Defclass *theClass)
  {
   return GetConstructModuleItem(&theClass->header);
  }

const char *DefclassModule(
  Defclass *theClass)
  {
   return GetConstructModuleName(&theClass->header);
  }

void SetDefclassPPForm(
  Environment *theEnv,
  Defclass *theClass,
  char *thePPForm)
  {
   SetConstructPPForm(theEnv,&theClass->header,thePPForm);
  }

#endif /* OBJECT_SYSTEM */