1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/16/14            */
5    /*                                                     */
6    /*          DEFTEMPLATE BASIC COMMANDS MODULE          */
7    /*******************************************************/
8 
9 /*************************************************************/
10 /* Purpose: Implements core commands for the deftemplate     */
11 /*   construct such as clear, reset, save, undeftemplate,    */
12 /*   ppdeftemplate, list-deftemplates, and                   */
13 /*   get-deftemplate-list.                                   */
14 /*                                                           */
15 /* Principal Programmer(s):                                  */
16 /*      Gary D. Riley                                        */
17 /*                                                           */
18 /* Contributing Programmer(s):                               */
19 /*      Brian L. Dantes                                      */
20 /*                                                           */
21 /* Revision History:                                         */
22 /*                                                           */
23 /*      6.23: Corrected compilation errors for files         */
24 /*            generated by constructs-to-c. DR0861           */
25 /*                                                           */
26 /*            Changed name of variable log to logName        */
27 /*            because of Unix compiler warnings of shadowed  */
28 /*            definitions.                                   */
29 /*                                                           */
30 /*      6.24: Renamed BOOLEAN macro type to intBool.         */
31 /*                                                           */
32 /*            Corrected code to remove compiler warnings     */
33 /*            when ENVIRONMENT_API_ONLY flag is set.         */
34 /*                                                           */
35 /*      6.30: Removed conditional code for unsupported       */
36 /*            compilers/operating systems (IBM_MCW,          */
37 /*            MAC_MCW, and IBM_TBC).                         */
38 /*                                                           */
39 /*            Added const qualifiers to remove C++           */
40 /*            deprecation warnings.                          */
41 /*                                                           */
42 /*            Converted API macros to function calls.        */
43 /*                                                           */
44 /*************************************************************/
45 
46 #define _TMPLTBSC_SOURCE_
47 
48 #include "setup.h"
49 
50 #if DEFTEMPLATE_CONSTRUCT
51 
52 #include <stdio.h>
53 #define _STDIO_INCLUDED_
54 #include <string.h>
55 
56 #include "argacces.h"
57 #include "memalloc.h"
58 #include "scanner.h"
59 #include "router.h"
60 #include "extnfunc.h"
61 #include "constrct.h"
62 #include "cstrccom.h"
63 #include "factrhs.h"
64 #include "cstrcpsr.h"
65 #include "tmpltpsr.h"
66 #include "tmpltdef.h"
67 #if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
68 #include "tmpltbin.h"
69 #endif
70 #if CONSTRUCT_COMPILER && (! RUN_TIME)
71 #include "tmpltcmp.h"
72 #endif
73 #include "tmpltutl.h"
74 #include "envrnmnt.h"
75 
76 #include "tmpltbsc.h"
77 
78 /***************************************/
79 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
80 /***************************************/
81 
82 #if ! DEFFACTS_CONSTRUCT
83    static void                    ResetDeftemplates(void *);
84 #endif
85    static void                    ClearDeftemplates(void *);
86    static void                    SaveDeftemplates(void *,void *,const char *);
87 
88 /*********************************************************************/
89 /* DeftemplateBasicCommands: Initializes basic deftemplate commands. */
90 /*********************************************************************/
DeftemplateBasicCommands(void * theEnv)91 globle void DeftemplateBasicCommands(
92   void *theEnv)
93   {
94 #if ! DEFFACTS_CONSTRUCT
95    EnvAddResetFunction(theEnv,"deftemplate",ResetDeftemplates,0);
96 #endif
97    EnvAddClearFunction(theEnv,"deftemplate",ClearDeftemplates,0);
98    AddSaveFunction(theEnv,"deftemplate",SaveDeftemplates,10);
99 
100 #if ! RUN_TIME
101    EnvDefineFunction2(theEnv,"get-deftemplate-list",'m',PTIEF GetDeftemplateListFunction,"GetDeftemplateListFunction","01w");
102    EnvDefineFunction2(theEnv,"undeftemplate",'v',PTIEF UndeftemplateCommand,"UndeftemplateCommand","11w");
103    EnvDefineFunction2(theEnv,"deftemplate-module",'w',PTIEF DeftemplateModuleFunction,"DeftemplateModuleFunction","11w");
104 
105 #if DEBUGGING_FUNCTIONS
106    EnvDefineFunction2(theEnv,"list-deftemplates",'v', PTIEF ListDeftemplatesCommand,"ListDeftemplatesCommand","01w");
107    EnvDefineFunction2(theEnv,"ppdeftemplate",'v',PTIEF PPDeftemplateCommand,"PPDeftemplateCommand","11w");
108 #endif
109 
110 #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
111    DeftemplateBinarySetup(theEnv);
112 #endif
113 
114 #if CONSTRUCT_COMPILER && (! RUN_TIME)
115    DeftemplateCompilerSetup(theEnv);
116 #endif
117 
118 #endif
119   }
120 
121 /*************************************************************/
122 /* ResetDeftemplates: Deftemplate reset routine for use with */
123 /*   the reset command. Asserts the initial-fact fact when   */
124 /*   the deffacts construct has been disabled.               */
125 /*************************************************************/
126 #if ! DEFFACTS_CONSTRUCT
ResetDeftemplates(void * theEnv)127 static void ResetDeftemplates(
128   void *theEnv)
129   {
130    struct fact *factPtr;
131 
132    factPtr = StringToFact(theEnv,"(initial-fact)");
133 
134    if (factPtr == NULL) return;
135 
136    EnvAssert(theEnv,(void *) factPtr);
137  }
138 #endif
139 
140 /*****************************************************************/
141 /* ClearDeftemplates: Deftemplate clear routine for use with the */
142 /*   clear command. Creates the initial-facts deftemplate.       */
143 /*****************************************************************/
ClearDeftemplates(void * theEnv)144 static void ClearDeftemplates(
145   void *theEnv)
146   {
147 #if (! RUN_TIME) && (! BLOAD_ONLY)
148 
149    CreateImpliedDeftemplate(theEnv,(SYMBOL_HN *) EnvAddSymbol(theEnv,"initial-fact"),FALSE);
150 #else
151 #if MAC_XCD
152 #pragma unused(theEnv)
153 #endif
154 #endif
155   }
156 
157 /**********************************************/
158 /* SaveDeftemplates: Deftemplate save routine */
159 /*   for use with the save command.           */
160 /**********************************************/
SaveDeftemplates(void * theEnv,void * theModule,const char * logicalName)161 static void SaveDeftemplates(
162   void *theEnv,
163   void *theModule,
164   const char *logicalName)
165   {
166    SaveConstruct(theEnv,theModule,logicalName,DeftemplateData(theEnv)->DeftemplateConstruct);
167   }
168 
169 /**********************************************/
170 /* UndeftemplateCommand: H/L access routine   */
171 /*   for the undeftemplate command.           */
172 /**********************************************/
UndeftemplateCommand(void * theEnv)173 globle void UndeftemplateCommand(
174   void *theEnv)
175   {
176    UndefconstructCommand(theEnv,"undeftemplate",DeftemplateData(theEnv)->DeftemplateConstruct);
177   }
178 
179 /**************************************/
180 /* EnvUndeftemplate: C access routine */
181 /*   for the undeftemplate command.   */
182 /**************************************/
EnvUndeftemplate(void * theEnv,void * theDeftemplate)183 globle intBool EnvUndeftemplate(
184   void *theEnv,
185   void *theDeftemplate)
186   {
187    return(Undefconstruct(theEnv,theDeftemplate,DeftemplateData(theEnv)->DeftemplateConstruct));
188   }
189 
190 /****************************************************/
191 /* GetDeftemplateListFunction: H/L access routine   */
192 /*   for the get-deftemplate-list function.         */
193 /****************************************************/
GetDeftemplateListFunction(void * theEnv,DATA_OBJECT_PTR returnValue)194 globle void GetDeftemplateListFunction(
195   void *theEnv,
196   DATA_OBJECT_PTR returnValue)
197   {
198    GetConstructListFunction(theEnv,"get-deftemplate-list",returnValue,DeftemplateData(theEnv)->DeftemplateConstruct);
199   }
200 
201 /***********************************************/
202 /* EnvGetDeftemplateList: C access routine for */
203 /*   the get-deftemplate-list function.        */
204 /***********************************************/
EnvGetDeftemplateList(void * theEnv,DATA_OBJECT_PTR returnValue,void * theModule)205 globle void EnvGetDeftemplateList(
206   void *theEnv,
207   DATA_OBJECT_PTR returnValue,
208   void *theModule)
209   {
210    GetConstructList(theEnv,returnValue,DeftemplateData(theEnv)->DeftemplateConstruct,(struct defmodule *) theModule);
211   }
212 
213 /***************************************************/
214 /* DeftemplateModuleFunction: H/L access routine   */
215 /*   for the deftemplate-module function.          */
216 /***************************************************/
DeftemplateModuleFunction(void * theEnv)217 globle void *DeftemplateModuleFunction(
218   void *theEnv)
219   {
220    return(GetConstructModuleCommand(theEnv,"deftemplate-module",DeftemplateData(theEnv)->DeftemplateConstruct));
221   }
222 
223 #if DEBUGGING_FUNCTIONS
224 
225 /**********************************************/
226 /* PPDeftemplateCommand: H/L access routine   */
227 /*   for the ppdeftemplate command.           */
228 /**********************************************/
PPDeftemplateCommand(void * theEnv)229 globle void PPDeftemplateCommand(
230   void *theEnv)
231   {
232    PPConstructCommand(theEnv,"ppdeftemplate",DeftemplateData(theEnv)->DeftemplateConstruct);
233   }
234 
235 /***************************************/
236 /* PPDeftemplate: C access routine for */
237 /*   the ppdeftemplate command.        */
238 /***************************************/
PPDeftemplate(void * theEnv,const char * deftemplateName,const char * logicalName)239 globle int PPDeftemplate(
240   void *theEnv,
241   const char *deftemplateName,
242   const char *logicalName)
243   {
244    return(PPConstruct(theEnv,deftemplateName,logicalName,DeftemplateData(theEnv)->DeftemplateConstruct));
245   }
246 
247 /*************************************************/
248 /* ListDeftemplatesCommand: H/L access routine   */
249 /*   for the list-deftemplates command.          */
250 /*************************************************/
ListDeftemplatesCommand(void * theEnv)251 globle void ListDeftemplatesCommand(
252   void *theEnv)
253   {
254    ListConstructCommand(theEnv,"list-deftemplates",DeftemplateData(theEnv)->DeftemplateConstruct);
255   }
256 
257 /*****************************************/
258 /* EnvListDeftemplates: C access routine */
259 /*   for the list-deftemplates command.  */
260 /*****************************************/
EnvListDeftemplates(void * theEnv,const char * logicalName,void * theModule)261 globle void EnvListDeftemplates(
262   void *theEnv,
263   const char *logicalName,
264   void *theModule)
265   {
266    ListConstruct(theEnv,DeftemplateData(theEnv)->DeftemplateConstruct,logicalName,(struct defmodule *) theModule);
267   }
268 
269 /***********************************************************/
270 /* EnvGetDeftemplateWatch: C access routine for retrieving */
271 /*   the current watch value of a deftemplate.             */
272 /***********************************************************/
EnvGetDeftemplateWatch(void * theEnv,void * theTemplate)273 globle unsigned EnvGetDeftemplateWatch(
274   void *theEnv,
275   void *theTemplate)
276   {
277 #if MAC_XCD
278 #pragma unused(theEnv)
279 #endif
280 
281    return(((struct deftemplate *) theTemplate)->watch);
282   }
283 
284 /*********************************************************/
285 /* EnvSetDeftemplateWatch:  C access routine for setting */
286 /*   the current watch value of a deftemplate.           */
287 /*********************************************************/
EnvSetDeftemplateWatch(void * theEnv,unsigned newState,void * theTemplate)288 globle void EnvSetDeftemplateWatch(
289   void *theEnv,
290   unsigned newState,
291   void *theTemplate)
292   {
293 #if MAC_XCD
294 #pragma unused(theEnv)
295 #endif
296 
297    ((struct deftemplate *) theTemplate)->watch = newState;
298   }
299 
300 /**********************************************************/
301 /* DeftemplateWatchAccess: Access routine for setting the */
302 /*   watch flag of a deftemplate via the watch command.   */
303 /**********************************************************/
DeftemplateWatchAccess(void * theEnv,int code,unsigned newState,EXPRESSION * argExprs)304 globle unsigned DeftemplateWatchAccess(
305   void *theEnv,
306   int code,
307   unsigned newState,
308   EXPRESSION *argExprs)
309   {
310 #if MAC_XCD
311 #pragma unused(code)
312 #endif
313 
314    return(ConstructSetWatchAccess(theEnv,DeftemplateData(theEnv)->DeftemplateConstruct,newState,argExprs,
315                                   EnvGetDeftemplateWatch,EnvSetDeftemplateWatch));
316   }
317 
318 /*************************************************************************/
319 /* DeftemplateWatchPrint: Access routine for printing which deftemplates */
320 /*   have their watch flag set via the list-watch-items command.         */
321 /*************************************************************************/
DeftemplateWatchPrint(void * theEnv,const char * logName,int code,EXPRESSION * argExprs)322 globle unsigned DeftemplateWatchPrint(
323   void *theEnv,
324   const char *logName,
325   int code,
326   EXPRESSION *argExprs)
327   {
328 #if MAC_XCD
329 #pragma unused(code)
330 #endif
331 
332    return(ConstructPrintWatchAccess(theEnv,DeftemplateData(theEnv)->DeftemplateConstruct,logName,argExprs,
333                                     EnvGetDeftemplateWatch,EnvSetDeftemplateWatch));
334   }
335 
336 #endif /* DEBUGGING_FUNCTIONS */
337 
338 /*#####################################*/
339 /* ALLOW_ENVIRONMENT_GLOBALS Functions */
340 /*#####################################*/
341 
342 #if ALLOW_ENVIRONMENT_GLOBALS
343 
GetDeftemplateList(DATA_OBJECT_PTR returnValue,void * theModule)344 globle void GetDeftemplateList(
345   DATA_OBJECT_PTR returnValue,
346   void *theModule)
347   {
348    EnvGetDeftemplateList(GetCurrentEnvironment(),returnValue,theModule);
349   }
350 
351 #if DEBUGGING_FUNCTIONS
352 
GetDeftemplateWatch(void * theTemplate)353 globle unsigned GetDeftemplateWatch(
354   void *theTemplate)
355   {
356    return EnvGetDeftemplateWatch(GetCurrentEnvironment(),theTemplate);
357   }
358 
ListDeftemplates(const char * logicalName,void * theModule)359 globle void ListDeftemplates(
360   const char *logicalName,
361   void *theModule)
362   {
363    EnvListDeftemplates(GetCurrentEnvironment(),logicalName,theModule);
364   }
365 
SetDeftemplateWatch(unsigned newState,void * theTemplate)366 globle void SetDeftemplateWatch(
367   unsigned newState,
368   void *theTemplate)
369   {
370    EnvSetDeftemplateWatch(GetCurrentEnvironment(),newState,theTemplate);
371   }
372 
373 #endif /* DEBUGGING_FUNCTIONS */
374 
Undeftemplate(void * theDeftemplate)375 globle intBool Undeftemplate(
376   void *theDeftemplate)
377   {
378    return EnvUndeftemplate(GetCurrentEnvironment(),theDeftemplate);
379   }
380 
381 #endif /* ALLOW_ENVIRONMENT_GLOBALS */
382 
383 
384 #endif /* DEFTEMPLATE_CONSTRUCT */
385 
386 
387