1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/22/14            */
5    /*                                                     */
6    /*         DEFMODULE BASIC COMMANDS HEADER FILE        */
7    /*******************************************************/
8 
9 /*************************************************************/
10 /* Purpose: Implements core commands for the defmodule       */
11 /*   construct such as clear, reset, save, ppdefmodule       */
12 /*   list-defmodules, and get-defmodule-list.                */
13 /*                                                           */
14 /* Principal Programmer(s):                                  */
15 /*      Gary D. Riley                                        */
16 /*                                                           */
17 /* Contributing Programmer(s):                               */
18 /*      Brian L. Dantes                                      */
19 /*                                                           */
20 /* Revision History:                                         */
21 /*                                                           */
22 /*      6.30: Removed conditional code for unsupported       */
23 /*            compilers/operating systems (IBM_MCW and       */
24 /*            MAC_MCW).                                      */
25 /*                                                           */
26 /*            Added const qualifiers to remove C++           */
27 /*            deprecation warnings.                          */
28 /*                                                           */
29 /*            Converted API macros to function calls.        */
30 /*                                                           */
31 /*************************************************************/
32 
33 #define _MODULBSC_SOURCE_
34 
35 #include "setup.h"
36 
37 #include <stdio.h>
38 #define _STDIO_INCLUDED_
39 #include <string.h>
40 
41 #include "constrct.h"
42 #include "extnfunc.h"
43 #include "modulbin.h"
44 #include "prntutil.h"
45 #include "modulcmp.h"
46 #include "router.h"
47 #include "argacces.h"
48 #include "bload.h"
49 #include "multifld.h"
50 #include "envrnmnt.h"
51 
52 #include "modulbsc.h"
53 
54 /***************************************/
55 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
56 /***************************************/
57 
58    static void                    ClearDefmodules(void *);
59 #if DEFMODULE_CONSTRUCT
60    static void                    SaveDefmodules(void *,void *,const char *);
61 #endif
62 
63 /*****************************************************************/
64 /* DefmoduleBasicCommands: Initializes basic defmodule commands. */
65 /*****************************************************************/
DefmoduleBasicCommands(void * theEnv)66 globle void DefmoduleBasicCommands(
67   void *theEnv)
68   {
69    EnvAddClearFunction(theEnv,"defmodule",ClearDefmodules,2000);
70 
71 #if DEFMODULE_CONSTRUCT
72    AddSaveFunction(theEnv,"defmodule",SaveDefmodules,1100);
73 
74 #if ! RUN_TIME
75    EnvDefineFunction2(theEnv,"get-defmodule-list",'m',PTIEF EnvGetDefmoduleList,"EnvGetDefmoduleList","00");
76 
77 #if DEBUGGING_FUNCTIONS
78    EnvDefineFunction2(theEnv,"list-defmodules",'v', PTIEF ListDefmodulesCommand,"ListDefmodulesCommand","00");
79    EnvDefineFunction2(theEnv,"ppdefmodule",'v',PTIEF PPDefmoduleCommand,"PPDefmoduleCommand","11w");
80 #endif
81 #endif
82 #endif
83 
84 #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
85    DefmoduleBinarySetup(theEnv);
86 #endif
87 
88 #if CONSTRUCT_COMPILER && (! RUN_TIME)
89    DefmoduleCompilerSetup(theEnv);
90 #endif
91   }
92 
93 /*********************************************************/
94 /* ClearDefmodules: Defmodule clear routine for use with */
95 /*   the clear command. Creates the MAIN module.         */
96 /*********************************************************/
ClearDefmodules(void * theEnv)97 static void ClearDefmodules(
98   void *theEnv)
99   {
100 #if (BLOAD || BLOAD_AND_BSAVE || BLOAD_ONLY) && (! RUN_TIME)
101    if (Bloaded(theEnv) == TRUE) return;
102 #endif
103 #if (! RUN_TIME)
104    RemoveAllDefmodules(theEnv);
105 
106    CreateMainModule(theEnv);
107    DefmoduleData(theEnv)->MainModuleRedefinable = TRUE;
108 #else
109 #if MAC_XCD
110 #pragma unused(theEnv)
111 #endif
112 #endif
113   }
114 
115 #if DEFMODULE_CONSTRUCT
116 
117 /******************************************/
118 /* SaveDefmodules: Defmodule save routine */
119 /*   for use with the save command.       */
120 /******************************************/
SaveDefmodules(void * theEnv,void * theModule,const char * logicalName)121 static void SaveDefmodules(
122   void *theEnv,
123   void *theModule,
124   const char *logicalName)
125   {
126    const char *ppform;
127 
128    ppform = EnvGetDefmodulePPForm(theEnv,theModule);
129    if (ppform != NULL)
130      {
131       PrintInChunks(theEnv,logicalName,ppform);
132       EnvPrintRouter(theEnv,logicalName,"\n");
133      }
134   }
135 
136 /*************************************************/
137 /* EnvGetDefmoduleList: H/L and C access routine */
138 /*   for the get-defmodule-list function.        */
139 /*************************************************/
EnvGetDefmoduleList(void * theEnv,DATA_OBJECT_PTR returnValue)140 globle void EnvGetDefmoduleList(
141   void *theEnv,
142   DATA_OBJECT_PTR returnValue)
143   {
144    void *theConstruct;
145    unsigned long count = 0;
146    struct multifield *theList;
147 
148    /*====================================*/
149    /* Determine the number of constructs */
150    /* of the specified type.             */
151    /*====================================*/
152 
153    for (theConstruct = EnvGetNextDefmodule(theEnv,NULL);
154         theConstruct != NULL;
155         theConstruct = EnvGetNextDefmodule(theEnv,theConstruct))
156      { count++; }
157 
158    /*===========================*/
159    /* Create a multifield large */
160    /* enough to store the list. */
161    /*===========================*/
162 
163    SetpType(returnValue,MULTIFIELD);
164    SetpDOBegin(returnValue,1);
165    SetpDOEnd(returnValue,(long) count);
166    theList = (struct multifield *) EnvCreateMultifield(theEnv,count);
167    SetpValue(returnValue,(void *) theList);
168 
169    /*====================================*/
170    /* Store the names in the multifield. */
171    /*====================================*/
172 
173    for (theConstruct = EnvGetNextDefmodule(theEnv,NULL), count = 1;
174         theConstruct != NULL;
175         theConstruct = EnvGetNextDefmodule(theEnv,theConstruct), count++)
176      {
177       if (EvaluationData(theEnv)->HaltExecution == TRUE)
178         {
179          EnvSetMultifieldErrorValue(theEnv,returnValue);
180          return;
181         }
182       SetMFType(theList,count,SYMBOL);
183       SetMFValue(theList,count,EnvAddSymbol(theEnv,EnvGetDefmoduleName(theEnv,theConstruct)));
184      }
185   }
186 
187 #if DEBUGGING_FUNCTIONS
188 
189 /********************************************/
190 /* PPDefmoduleCommand: H/L access routine   */
191 /*   for the ppdefmodule command.           */
192 /********************************************/
PPDefmoduleCommand(void * theEnv)193 globle void PPDefmoduleCommand(
194   void *theEnv)
195   {
196    const char *defmoduleName;
197 
198    defmoduleName = GetConstructName(theEnv,"ppdefmodule","defmodule name");
199    if (defmoduleName == NULL) return;
200 
201    PPDefmodule(theEnv,defmoduleName,WDISPLAY);
202 
203    return;
204   }
205 
206 /*************************************/
207 /* PPDefmodule: C access routine for */
208 /*   the ppdefmodule command.        */
209 /*************************************/
PPDefmodule(void * theEnv,const char * defmoduleName,const char * logicalName)210 globle int PPDefmodule(
211   void *theEnv,
212   const char *defmoduleName,
213   const char *logicalName)
214   {
215    void *defmodulePtr;
216 
217    defmodulePtr = EnvFindDefmodule(theEnv,defmoduleName);
218    if (defmodulePtr == NULL)
219      {
220       CantFindItemErrorMessage(theEnv,"defmodule",defmoduleName);
221       return(FALSE);
222      }
223 
224    if (EnvGetDefmodulePPForm(theEnv,defmodulePtr) == NULL) return(TRUE);
225    PrintInChunks(theEnv,logicalName,EnvGetDefmodulePPForm(theEnv,defmodulePtr));
226    return(TRUE);
227   }
228 
229 /***********************************************/
230 /* ListDefmodulesCommand: H/L access routine   */
231 /*   for the list-defmodules command.          */
232 /***********************************************/
ListDefmodulesCommand(void * theEnv)233 globle void ListDefmodulesCommand(
234   void *theEnv)
235   {
236    if (EnvArgCountCheck(theEnv,"list-defmodules",EXACTLY,0) == -1) return;
237 
238    EnvListDefmodules(theEnv,WDISPLAY);
239   }
240 
241 /***************************************/
242 /* EnvListDefmodules: C access routine */
243 /*   for the list-defmodules command.  */
244 /***************************************/
EnvListDefmodules(void * theEnv,const char * logicalName)245 globle void EnvListDefmodules(
246   void *theEnv,
247   const char *logicalName)
248   {
249    void *theModule;
250    int count = 0;
251 
252    for (theModule = EnvGetNextDefmodule(theEnv,NULL);
253         theModule != NULL;
254         theModule = EnvGetNextDefmodule(theEnv,theModule))
255     {
256      EnvPrintRouter(theEnv,logicalName,EnvGetDefmoduleName(theEnv,theModule));
257      EnvPrintRouter(theEnv,logicalName,"\n");
258      count++;
259     }
260 
261    PrintTally(theEnv,logicalName,count,"defmodule","defmodules");
262   }
263 
264 #endif /* DEBUGGING_FUNCTIONS */
265 
266 /*#####################################*/
267 /* ALLOW_ENVIRONMENT_GLOBALS Functions */
268 /*#####################################*/
269 
270 #if ALLOW_ENVIRONMENT_GLOBALS
271 
GetDefmoduleList(DATA_OBJECT_PTR returnValue)272 globle void GetDefmoduleList(
273   DATA_OBJECT_PTR returnValue)
274   {
275    EnvGetDefmoduleList(GetCurrentEnvironment(),returnValue);
276   }
277 
278 #if DEBUGGING_FUNCTIONS
279 
ListDefmodules(const char * logicalName)280 globle void ListDefmodules(
281   const char *logicalName)
282   {
283    EnvListDefmodules(GetCurrentEnvironment(),logicalName);
284   }
285 
286 #endif /* DEBUGGING_FUNCTIONS */
287 
288 #endif /* ALLOW_ENVIRONMENT_GLOBALS */
289 
290 #endif /* DEFMODULE_CONSTRUCT */
291 
292 
293