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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 09/20/17 */
/* */
/* ARGUMENT ACCESS MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides access routines for accessing arguments */
/* passed to user or system functions defined using the */
/* DefineFunction protocol. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/* Added IllegalLogicalNameMessage function. */
/* */
/* 6.30: Support for long long integers. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* Converted API macros to function calls. */
/* */
/* Support for fact-address arguments. */
/* */
/* 6.31: Modified the GetFactOrInstanceArgument */
/* function so that error messages are now */
/* generated when the timetag, dependencies, and */
/* dependents functions are given a retracted */
/* fact. */
/* */
/* 6.40: Added Env prefix to GetEvaluationError and */
/* SetEvaluationError functions. */
/* */
/* 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. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/* UDF redesign. */
/* */
/*************************************************************/
#include "setup.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "cstrnchk.h"
#include "envrnmnt.h"
#include "extnfunc.h"
#include "factmngr.h"
#include "insfun.h"
#include "prntutil.h"
#include "router.h"
#include "sysdep.h"
#include "argacces.h"
/*********************************************************************/
/* GetLogicalName: Retrieves the nth argument passed to the function */
/* call currently being evaluated and determines if it is a valid */
/* logical name. If valid, the logical name is returned, otherwise */
/* NULL is returned. */
/*********************************************************************/
const char *GetLogicalName(
UDFContext *context,
const char *defaultLogicalName)
{
Environment *theEnv = context->environment;
const char *logicalName;
UDFValue theArg;
if (! UDFNextArgument(context,ANY_TYPE_BITS,&theArg))
{ return NULL; }
if (CVIsType(&theArg,LEXEME_BITS) ||
CVIsType(&theArg,INSTANCE_NAME_BIT))
{
logicalName = theArg.lexemeValue->contents;
if ((strcmp(logicalName,"t") == 0) || (strcmp(logicalName,"T") == 0))
{ logicalName = defaultLogicalName; }
}
else if (CVIsType(&theArg,FLOAT_BIT))
{
logicalName = CreateSymbol(theEnv,FloatToString(theEnv,theArg.floatValue->contents))->contents;
}
else if (CVIsType(&theArg,INTEGER_BIT))
{
logicalName = CreateSymbol(theEnv,LongIntegerToString(theEnv,theArg.integerValue->contents))->contents;
}
else
{ logicalName = NULL; }
return(logicalName);
}
/************************************************************/
/* GetFileName: Retrieves the nth argument passed to the */
/* function call currently being evaluated and determines */
/* if it is a valid file name. If valid, the file name is */
/* returned, otherwise NULL is returned. */
/************************************************************/
const char *GetFileName(
UDFContext *context)
{
UDFValue theArg;
if (! UDFNextArgument(context,LEXEME_BITS,&theArg))
{ return NULL; }
return theArg.lexemeValue->contents;
}
/******************************************************************/
/* OpenErrorMessage: Generalized error message for opening files. */
/******************************************************************/
void OpenErrorMessage(
Environment *theEnv,
const char *functionName,
const char *fileName)
{
PrintErrorID(theEnv,"ARGACCES",3,false);
WriteString(theEnv,STDERR,"Function '");
WriteString(theEnv,STDERR,functionName);
WriteString(theEnv,STDERR,"' was unable to open file '");
WriteString(theEnv,STDERR,fileName);
WriteString(theEnv,STDERR,"'.\n");
}
/************************************************************/
/* GetModuleName: Retrieves the nth argument passed to the */
/* function call currently being evaluated and determines */
/* if it is a valid module name. If valid, the module */
/* name is returned or NULL is returned to indicate all */
/* modules. */
/************************************************************/
Defmodule *GetModuleName(
UDFContext *context,
unsigned int whichArgument,
bool *error)
{
UDFValue returnValue;
Defmodule *theModule;
Environment *theEnv = context->environment;
const char *functionName = UDFContextFunctionName(context);
*error = false;
/*========================*/
/* Retrieve the argument. */
/*========================*/
if (! UDFNthArgument(context,1,SYMBOL_BIT,&returnValue))
{
*error = true;
return NULL;
}
/*=======================================*/
/* Check to see that the symbol actually */
/* corresponds to a defined module. */
/*=======================================*/
if ((theModule = FindDefmodule(theEnv,returnValue.lexemeValue->contents)) == NULL)
{
if (strcmp("*",returnValue.lexemeValue->contents) != 0)
{
ExpectedTypeError1(theEnv,functionName,whichArgument,"'defmodule name'");
*error = true;
}
return NULL;
}
/*=================================*/
/* Return a pointer to the module. */
/*=================================*/
return(theModule);
}
/****************************************************************/
/* GetConstructName: Retrieves the 1st argument passed to the */
/* function call currently being evaluated and determines if */
/* it is a valid name for a construct. Also checks that the */
/* function is only passed a single argument. This routine */
/* is used by functions such as ppdeftemplate, undefrule, */
/* etc... to retrieve the construct name on which to operate. */
/****************************************************************/
const char *GetConstructName(
UDFContext *context,
const char *functionName,
const char *constructType)
{
UDFValue returnValue;
if (! UDFFirstArgument(context,ANY_TYPE_BITS,&returnValue))
{ return NULL; }
if (! CVIsType(&returnValue,SYMBOL_BIT))
{
UDFInvalidArgumentMessage(context,constructType);
return NULL;
}
return(returnValue.lexemeValue->contents);
}
/*********************************************************/
/* ExpectedCountError: Prints the error message for an */
/* incorrect number of arguments passed to a function. */
/*********************************************************/
void ExpectedCountError(
Environment *theEnv,
const char *functionName,
int countRelation,
unsigned int expectedNumber)
{
PrintErrorID(theEnv,"ARGACCES",1,false);
WriteString(theEnv,STDERR,"Function '");
WriteString(theEnv,STDERR,functionName);
WriteString(theEnv,STDERR,"'");
if (countRelation == EXACTLY)
{ WriteString(theEnv,STDERR," expected exactly "); }
else if (countRelation == AT_LEAST)
{ WriteString(theEnv,STDERR," expected at least "); }
else if (countRelation == NO_MORE_THAN)
{ WriteString(theEnv,STDERR," expected no more than "); }
else
{ WriteString(theEnv,STDERR," generated an illegal argument check for "); }
PrintUnsignedInteger(theEnv,STDERR,expectedNumber);
if (expectedNumber == 1)
{ WriteString(theEnv,STDERR," argument.\n"); }
else
{ WriteString(theEnv,STDERR," arguments.\n"); }
}
/*************************************************************/
/* NAME : CheckFunctionArgCount */
/* DESCRIPTION : Checks the number of arguments against */
/* the system function restriction list */
/* INPUTS : 1) Name of the calling function */
/* 2) The restriction list can be NULL */
/* 3) The number of arguments */
/* RETURNS : True if OK, false otherwise */
/* SIDE EFFECTS : EvaluationError set on errrors */
/* NOTES : Used to check generic function implicit */
/* method (system function) calls and system */
/* function calls which have the sequence */
/* expansion operator in their argument list */
/*************************************************************/
bool CheckFunctionArgCount(
Environment *theEnv,
struct functionDefinition *func,
int argumentCount)
{
unsigned short minArguments, maxArguments;
const char *functionName;
functionName = func->callFunctionName->contents;
/*===========================================*/
/* Determine the minimum number of arguments */
/* required by the function. */
/*===========================================*/
minArguments = func->minArgs;
/*===========================================*/
/* Determine the maximum number of arguments */
/* required by the function. */
/*===========================================*/
maxArguments = func->maxArgs;
/*=====================================*/
/* If the function has no restrictions */
/* on function arguments, return true. */
/*=====================================*/
if ((minArguments == UNBOUNDED) && (maxArguments == UNBOUNDED))
{ return true; }
/*==============================================*/
/* If the function expects exactly N arguments, */
/* then check to see if there are N arguments. */
/*==============================================*/
if (minArguments == maxArguments)
{
if (argumentCount != minArguments)
{
ExpectedCountError(theEnv,functionName,EXACTLY,minArguments);
SetEvaluationError(theEnv,true);
return false;
}
return true;
}
/*==================================*/
/* Check to see if there were fewer */
/* arguments passed than expected. */
/*==================================*/
if (argumentCount < minArguments)
{
ExpectedCountError(theEnv,functionName,AT_LEAST,minArguments);
SetEvaluationError(theEnv,true);
return false;
}
/*=================================*/
/* Check to see if there were more */
/* arguments passed than expected. */
/*=================================*/
if ((maxArguments != UNBOUNDED) && (argumentCount > maxArguments))
{
ExpectedCountError(theEnv,functionName,NO_MORE_THAN,maxArguments);
SetEvaluationError(theEnv,true);
return false;
}
/*===============================*/
/* The number of arguments falls */
/* within the expected range. */
/*===============================*/
return true;
}
/*******************************************************************/
/* ExpectedTypeError0: Prints the error message for the wrong type */
/* of argument passed to a user or system defined function. */
/*******************************************************************/
void ExpectedTypeError0(
Environment *theEnv,
const char *functionName,
unsigned int whichArg)
{
PrintErrorID(theEnv,"ARGACCES",2,false);
WriteString(theEnv,STDERR,"Function '");
WriteString(theEnv,STDERR,functionName);
WriteString(theEnv,STDERR,"' expected argument #");
WriteInteger(theEnv,STDERR,whichArg);
WriteString(theEnv,STDERR," to be of type ");
}
/*******************************************************************/
/* ExpectedTypeError1: Prints the error message for the wrong type */
/* of argument passed to a user or system defined function. The */
/* expected type is passed as a string to this function. */
/*******************************************************************/
void ExpectedTypeError1(
Environment *theEnv,
const char *functionName,
unsigned int whichArg,
const char *expectedType)
{
ExpectedTypeError0(theEnv,functionName,whichArg);
WriteString(theEnv,STDERR,expectedType);
WriteString(theEnv,STDERR,".\n");
}
/**************************************************************/
/* ExpectedTypeError2: Prints the error message for the wrong */
/* type of argument passed to a user or system defined */
/* function. The expected type is derived by examining the */
/* function's argument restriction list. */
/**************************************************************/
void ExpectedTypeError2(
Environment *theEnv,
const char *functionName,
unsigned int whichArg)
{
unsigned theRestriction;
struct functionDefinition *theFunction;
theFunction = FindFunction(theEnv,functionName);
if (theFunction == NULL) return;
theRestriction = GetNthRestriction(theEnv,theFunction,whichArg);
ExpectedTypeError0(theEnv,functionName,whichArg);
PrintTypesString(theEnv,STDERR,theRestriction,true);
}
/***************************************************/
/* GetFactOrInstanceArgument: Utility routine for */
/* retrieving a fact or instance argument */
/***************************************************/
void *GetFactOrInstanceArgument(
UDFContext *context,
unsigned int thePosition,
UDFValue *item)
{
Environment *theEnv = context->environment;
#if DEFTEMPLATE_CONSTRUCT || OBJECT_SYSTEM
void *ptr;
#endif
/*==============================*/
/* Retrieve the first argument. */
/*==============================*/
UDFNthArgument(context,thePosition,ANY_TYPE_BITS,item);
/*==================================================*/
/* Fact and instance addresses are valid arguments. */
/*==================================================*/
if (CVIsType(item,FACT_ADDRESS_BIT))
{
if (item->factValue->garbage)
{
FactRetractedErrorMessage(theEnv,item->factValue);
return NULL;
}
return item->value;
}
else if (CVIsType(item,INSTANCE_ADDRESS_BIT))
{
if (item->instanceValue->garbage)
{
CantFindItemErrorMessage(theEnv,"instance",item->instanceValue->name->contents,false);
return NULL;
}
return item->value;
}
/*==================================================*/
/* An integer is a valid argument if it corresponds */
/* to the fact index of an existing fact. */
/*==================================================*/
#if DEFTEMPLATE_CONSTRUCT
else if (item->header->type == INTEGER_TYPE)
{
if ((ptr = (void *) FindIndexedFact(theEnv,item->integerValue->contents)) == NULL)
{
char tempBuffer[20];
gensprintf(tempBuffer,"f-%lld",item->integerValue->contents);
CantFindItemErrorMessage(theEnv,"fact",tempBuffer,false);
}
return ptr;
}
#endif
/*================================================*/
/* Instance names and symbols are valid arguments */
/* if they correspond to an existing instance. */
/*================================================*/
#if OBJECT_SYSTEM
else if (CVIsType(item,INSTANCE_NAME_BIT | SYMBOL_BIT))
{
if ((ptr = (void *) FindInstanceBySymbol(theEnv,item->lexemeValue)) == NULL)
{
CantFindItemErrorMessage(theEnv,"instance",item->lexemeValue->contents,false);
}
return ptr;
}
#endif
/*========================================*/
/* Any other type is an invalid argument. */
/*========================================*/
ExpectedTypeError2(theEnv,UDFContextFunctionName(context),thePosition);
return NULL;
}
/****************************************************/
/* IllegalLogicalNameMessage: Generic error message */
/* for illegal logical names. */
/****************************************************/
void IllegalLogicalNameMessage(
Environment *theEnv,
const char *theFunction)
{
PrintErrorID(theEnv,"IOFUN",1,false);
WriteString(theEnv,STDERR,"Illegal logical name used for '");
WriteString(theEnv,STDERR,theFunction);
WriteString(theEnv,STDERR,"' function.\n");
}