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
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 03/20/19 */
/* */
/* I/O ROUTER MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides a centralized mechanism for handling */
/* input and output requests. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Removed conversion of '\r' to '\n' from the */
/* EnvGetcRouter function. */
/* */
/* Renamed BOOLEAN macro type to intBool. */
/* */
/* Added support for passing context information */
/* to the router functions. */
/* */
/* 6.30: Fixed issues with passing context to routers. */
/* */
/* Added AwaitingInput flag. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* Converted API macros to function calls. */
/* */
/* 6.31: Fixed line count issue when using Windows */
/* line endings in Unix. */
/* */
/* 6.40: Added InputBufferCount function. */
/* */
/* Added check for reuse of existing router name. */
/* */
/* Added Env prefix to GetEvaluationError and */
/* SetEvaluationError functions. */
/* */
/* Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Changed return values for router functions. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/* Callbacks must be environment aware. */
/* */
/* UDF redesign. */
/* */
/*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "setup.h"
#include "argacces.h"
#include "constant.h"
#include "envrnmnt.h"
#include "extnfunc.h"
#include "filertr.h"
#include "memalloc.h"
#include "prntutil.h"
#include "scanner.h"
#include "strngrtr.h"
#include "sysdep.h"
#include "router.h"
/**********************/
/* STRING DEFINITIONS */
/**********************/
const char *STDIN = "stdin";
const char *STDOUT = "stdout";
const char *STDERR = "stderr";
const char *STDWRN = "stdwrn";
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static bool QueryRouter(Environment *,const char *,struct router *);
static void DeallocateRouterData(Environment *);
/*********************************************************/
/* InitializeDefaultRouters: Initializes output streams. */
/*********************************************************/
void InitializeDefaultRouters(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,ROUTER_DATA,sizeof(struct routerData),DeallocateRouterData);
RouterData(theEnv)->CommandBufferInputCount = 0;
RouterData(theEnv)->InputUngets = 0;
RouterData(theEnv)->AwaitingInput = true;
InitializeFileRouter(theEnv);
InitializeStringRouter(theEnv);
}
/*************************************************/
/* DeallocateRouterData: Deallocates environment */
/* data for I/O routers. */
/*************************************************/
static void DeallocateRouterData(
Environment *theEnv)
{
struct router *tmpPtr, *nextPtr;
tmpPtr = RouterData(theEnv)->ListOfRouters;
while (tmpPtr != NULL)
{
nextPtr = tmpPtr->next;
genfree(theEnv,(void *) tmpPtr->name,strlen(tmpPtr->name) + 1);
rtn_struct(theEnv,router,tmpPtr);
tmpPtr = nextPtr;
}
}
/*********************/
/* PrintRouterExists */
/*********************/
bool PrintRouterExists(
Environment *theEnv,
const char *logicalName)
{
struct router *currentPtr;
if (((char *) RouterData(theEnv)->FastSaveFilePtr) == logicalName)
{ return true; }
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if ((currentPtr->writeCallback != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : false)
{ return true; }
currentPtr = currentPtr->next;
}
return false;
}
/**********************************/
/* Write: Generic print function. */
/**********************************/
void Write(
Environment *theEnv,
const char *str)
{
WriteString(theEnv,STDOUT,str);
}
/************************************/
/* Writeln: Generic print function. */
/************************************/
void Writeln(
Environment *theEnv,
const char *str)
{
WriteString(theEnv,STDOUT,str);
WriteString(theEnv,STDOUT,"\n");
}
/****************************************/
/* WriteString: Generic print function. */
/****************************************/
void WriteString(
Environment *theEnv,
const char *logicalName,
const char *str)
{
struct router *currentPtr;
if (str == NULL) return;
/*===================================================*/
/* If the "fast save" option is being used, then the */
/* logical name is actually a pointer to a file and */
/* fprintf can be called directly to bypass querying */
/* all of the routers. */
/*===================================================*/
if (((char *) RouterData(theEnv)->FastSaveFilePtr) == logicalName)
{
fprintf(RouterData(theEnv)->FastSaveFilePtr,"%s",str);
return;
}
/*==============================================*/
/* Search through the list of routers until one */
/* is found that will handle the print request. */
/*==============================================*/
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if ((currentPtr->writeCallback != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : false)
{
(*currentPtr->writeCallback)(theEnv,logicalName,str,currentPtr->context);
return;
}
currentPtr = currentPtr->next;
}
/*=====================================================*/
/* The logical name was not recognized by any routers. */
/*=====================================================*/
if (strcmp(STDERR,logicalName) != 0)
{ UnrecognizedRouterMessage(theEnv,logicalName); }
}
/***********************************************/
/* ReadRouter: Generic get character function. */
/***********************************************/
int ReadRouter(
Environment *theEnv,
const char *logicalName)
{
struct router *currentPtr;
int inchar;
/*===================================================*/
/* If the "fast load" option is being used, then the */
/* logical name is actually a pointer to a file and */
/* getc can be called directly to bypass querying */
/* all of the routers. */
/*===================================================*/
if (((char *) RouterData(theEnv)->FastLoadFilePtr) == logicalName)
{
inchar = getc(RouterData(theEnv)->FastLoadFilePtr);
if (inchar == '\n')
{
if (((char *) RouterData(theEnv)->FastLoadFilePtr) == RouterData(theEnv)->LineCountRouter)
{ IncrementLineCount(theEnv); }
}
/* if (inchar == '\r') return('\n'); */
return(inchar);
}
/*===============================================*/
/* If the "fast string get" option is being used */
/* for the specified logical name, then bypass */
/* the router system and extract the character */
/* directly from the fast get string. */
/*===============================================*/
if (RouterData(theEnv)->FastCharGetRouter == logicalName)
{
inchar = (unsigned char) RouterData(theEnv)->FastCharGetString[RouterData(theEnv)->FastCharGetIndex];
RouterData(theEnv)->FastCharGetIndex++;
if (inchar == '\0') return(EOF);
if (inchar == '\n')
{
if (RouterData(theEnv)->FastCharGetRouter == RouterData(theEnv)->LineCountRouter)
{ IncrementLineCount(theEnv); }
}
return(inchar);
}
/*==============================================*/
/* Search through the list of routers until one */
/* is found that will handle the getc request. */
/*==============================================*/
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if ((currentPtr->readCallback != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : false)
{
inchar = (*currentPtr->readCallback)(theEnv,logicalName,currentPtr->context);
if (inchar == '\n')
{
if ((RouterData(theEnv)->LineCountRouter != NULL) &&
(strcmp(logicalName,RouterData(theEnv)->LineCountRouter) == 0))
{ IncrementLineCount(theEnv); }
}
return(inchar);
}
currentPtr = currentPtr->next;
}
/*=====================================================*/
/* The logical name was not recognized by any routers. */
/*=====================================================*/
UnrecognizedRouterMessage(theEnv,logicalName);
return(-1);
}
/***************************************************/
/* UnreadRouter: Generic unget character function. */
/***************************************************/
int UnreadRouter(
Environment *theEnv,
const char *logicalName,
int ch)
{
struct router *currentPtr;
/*===================================================*/
/* If the "fast load" option is being used, then the */
/* logical name is actually a pointer to a file and */
/* ungetc can be called directly to bypass querying */
/* all of the routers. */
/*===================================================*/
if (((char *) RouterData(theEnv)->FastLoadFilePtr) == logicalName)
{
if (ch == '\n')
{
if (((char *) RouterData(theEnv)->FastLoadFilePtr) == RouterData(theEnv)->LineCountRouter)
{ DecrementLineCount(theEnv); }
}
return ungetc(ch,RouterData(theEnv)->FastLoadFilePtr);
}
/*===============================================*/
/* If the "fast string get" option is being used */
/* for the specified logical name, then bypass */
/* the router system and unget the character */
/* directly from the fast get string. */
/*===============================================*/
if (RouterData(theEnv)->FastCharGetRouter == logicalName)
{
if (ch == '\n')
{
if (RouterData(theEnv)->FastCharGetRouter == RouterData(theEnv)->LineCountRouter)
{ DecrementLineCount(theEnv); }
}
if (RouterData(theEnv)->FastCharGetIndex > 0) RouterData(theEnv)->FastCharGetIndex--;
return ch;
}
/*===============================================*/
/* Search through the list of routers until one */
/* is found that will handle the ungetc request. */
/*===============================================*/
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if ((currentPtr->unreadCallback != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : false)
{
if (ch == '\n')
{
if ((RouterData(theEnv)->LineCountRouter != NULL) &&
(strcmp(logicalName,RouterData(theEnv)->LineCountRouter) == 0))
{ DecrementLineCount(theEnv); }
}
return (*currentPtr->unreadCallback)(theEnv,logicalName,ch,currentPtr->context);
}
currentPtr = currentPtr->next;
}
/*=====================================================*/
/* The logical name was not recognized by any routers. */
/*=====================================================*/
UnrecognizedRouterMessage(theEnv,logicalName);
return -1;
}
/********************************************/
/* ExitRouter: Generic exit function. Calls */
/* all of the router exit functions. */
/********************************************/
void ExitRouter(
Environment *theEnv,
int num)
{
struct router *currentPtr, *nextPtr;
RouterData(theEnv)->Abort = false;
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
nextPtr = currentPtr->next;
if (currentPtr->active == true)
{
if (currentPtr->exitCallback != NULL)
{
(*currentPtr->exitCallback)(theEnv,num,currentPtr->context);
}
}
currentPtr = nextPtr;
}
if (RouterData(theEnv)->Abort) return;
genexit(theEnv,num);
}
/********************************************/
/* AbortExit: Forces ExitRouter to terminate */
/* after calling all closing routers. */
/********************************************/
void AbortExit(
Environment *theEnv)
{
RouterData(theEnv)->Abort = true;
}
/*********************************************************/
/* AddRouter: Adds an I/O router to the list of routers. */
/*********************************************************/
bool AddRouter(
Environment *theEnv,
const char *routerName,
int priority,
RouterQueryFunction *queryFunction,
RouterWriteFunction *writeFunction,
RouterReadFunction *readFunction,
RouterUnreadFunction *unreadFunction,
RouterExitFunction *exitFunction,
void *context)
{
struct router *newPtr, *lastPtr, *currentPtr;
char *nameCopy;
/*==================================================*/
/* Reject the router if the name is already in use. */
/*==================================================*/
for (currentPtr = RouterData(theEnv)->ListOfRouters;
currentPtr != NULL;
currentPtr = currentPtr->next)
{
if (strcmp(currentPtr->name,routerName) == 0)
{ return false; }
}
newPtr = get_struct(theEnv,router);
nameCopy = (char *) genalloc(theEnv,strlen(routerName) + 1);
genstrcpy(nameCopy,routerName);
newPtr->name = nameCopy;
newPtr->active = true;
newPtr->context = context;
newPtr->priority = priority;
newPtr->queryCallback = queryFunction;
newPtr->writeCallback = writeFunction;
newPtr->exitCallback = exitFunction;
newPtr->readCallback = readFunction;
newPtr->unreadCallback = unreadFunction;
newPtr->next = NULL;
if (RouterData(theEnv)->ListOfRouters == NULL)
{
RouterData(theEnv)->ListOfRouters = newPtr;
return true;
}
lastPtr = NULL;
currentPtr = RouterData(theEnv)->ListOfRouters;
while ((currentPtr != NULL) ? (priority < currentPtr->priority) : false)
{
lastPtr = currentPtr;
currentPtr = currentPtr->next;
}
if (lastPtr == NULL)
{
newPtr->next = RouterData(theEnv)->ListOfRouters;
RouterData(theEnv)->ListOfRouters = newPtr;
}
else
{
newPtr->next = currentPtr;
lastPtr->next = newPtr;
}
return true;
}
/*****************************************************************/
/* DeleteRouter: Removes an I/O router from the list of routers. */
/*****************************************************************/
bool DeleteRouter(
Environment *theEnv,
const char *routerName)
{
struct router *currentPtr, *lastPtr;
currentPtr = RouterData(theEnv)->ListOfRouters;
lastPtr = NULL;
while (currentPtr != NULL)
{
if (strcmp(currentPtr->name,routerName) == 0)
{
genfree(theEnv,(void *) currentPtr->name,strlen(currentPtr->name) + 1);
if (lastPtr == NULL)
{
RouterData(theEnv)->ListOfRouters = currentPtr->next;
rm(theEnv,currentPtr,sizeof(struct router));
return true;
}
lastPtr->next = currentPtr->next;
rm(theEnv,currentPtr,sizeof(struct router));
return true;
}
lastPtr = currentPtr;
currentPtr = currentPtr->next;
}
return false;
}
/*********************************************************************/
/* QueryRouters: Determines if any router recognizes a logical name. */
/*********************************************************************/
bool QueryRouters(
Environment *theEnv,
const char *logicalName)
{
struct router *currentPtr;
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if (QueryRouter(theEnv,logicalName,currentPtr) == true) return true;
currentPtr = currentPtr->next;
}
return false;
}
/************************************************/
/* QueryRouter: Determines if a specific router */
/* recognizes a logical name. */
/************************************************/
static bool QueryRouter(
Environment *theEnv,
const char *logicalName,
struct router *currentPtr)
{
/*===================================================*/
/* If the router is inactive, then it can't respond. */
/*===================================================*/
if (currentPtr->active == false)
{ return false; }
/*=============================================================*/
/* If the router has no query function, then it can't respond. */
/*=============================================================*/
if (currentPtr->queryCallback == NULL) return false;
/*=========================================*/
/* Call the router's query function to see */
/* if it recognizes the logical name. */
/*=========================================*/
if ((*currentPtr->queryCallback)(theEnv,logicalName,currentPtr->context) == true)
{ return true; }
return false;
}
/*******************************************************/
/* DeactivateRouter: Deactivates a specific router. */
/*******************************************************/
bool DeactivateRouter(
Environment *theEnv,
const char *routerName)
{
struct router *currentPtr;
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if (strcmp(currentPtr->name,routerName) == 0)
{
currentPtr->active = false;
return true;
}
currentPtr = currentPtr->next;
}
return false;
}
/************************************************/
/* ActivateRouter: Activates a specific router. */
/************************************************/
bool ActivateRouter(
Environment *theEnv,
const char *routerName)
{
struct router *currentPtr;
currentPtr = RouterData(theEnv)->ListOfRouters;
while (currentPtr != NULL)
{
if (strcmp(currentPtr->name,routerName) == 0)
{
currentPtr->active = true;
return true;
}
currentPtr = currentPtr->next;
}
return false;
}
/*****************************************/
/* FindRouter: Locates the named router. */
/*****************************************/
Router *FindRouter(
Environment *theEnv,
const char *routerName)
{
Router *currentPtr;
for (currentPtr = RouterData(theEnv)->ListOfRouters;
currentPtr != NULL;
currentPtr = currentPtr->next)
{
if (strcmp(currentPtr->name,routerName) == 0)
{ return currentPtr; }
}
return NULL;
}
/********************************************************/
/* SetFastLoad: Used to bypass router system for loads. */
/********************************************************/
void SetFastLoad(
Environment *theEnv,
FILE *filePtr)
{
RouterData(theEnv)->FastLoadFilePtr = filePtr;
}
/********************************************************/
/* SetFastSave: Used to bypass router system for saves. */
/********************************************************/
void SetFastSave(
Environment *theEnv,
FILE *filePtr)
{
RouterData(theEnv)->FastSaveFilePtr = filePtr;
}
/******************************************************/
/* GetFastLoad: Returns the "fast load" file pointer. */
/******************************************************/
FILE *GetFastLoad(
Environment *theEnv)
{
return(RouterData(theEnv)->FastLoadFilePtr);
}
/******************************************************/
/* GetFastSave: Returns the "fast save" file pointer. */
/******************************************************/
FILE *GetFastSave(
Environment *theEnv)
{
return(RouterData(theEnv)->FastSaveFilePtr);
}
/*****************************************************/
/* UnrecognizedRouterMessage: Standard error message */
/* for an unrecognized router name. */
/*****************************************************/
void UnrecognizedRouterMessage(
Environment *theEnv,
const char *logicalName)
{
PrintErrorID(theEnv,"ROUTER",1,false);
WriteString(theEnv,STDERR,"Logical name '");
WriteString(theEnv,STDERR,logicalName);
WriteString(theEnv,STDERR,"' was not recognized by any routers.\n");
}
/*****************************************/
/* PrintNRouter: Generic print function. */
/*****************************************/
void PrintNRouter(
Environment *theEnv,
const char *logicalName,
const char *str,
unsigned long length)
{
char *tempStr;
tempStr = (char *) genalloc(theEnv,length+1);
genstrncpy(tempStr,str,length);
tempStr[length] = 0;
WriteString(theEnv,logicalName,tempStr);
genfree(theEnv,tempStr,length+1);
}
/*********************/
/* InputBufferCount: */
/*********************/
size_t InputBufferCount(
Environment *theEnv)
{
return RouterData(theEnv)->CommandBufferInputCount;
}