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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 03/20/19 */
/* */
/* FILE UTILITY MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Contains the code for file commands including */
/* batch, dribble-on, dribble-off, save, load, bsave, and */
/* bload. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* Brian L. Dantes */
/* */
/* Contributing Programmer(s): */
/* Bebe Ly */
/* */
/* Revision History: */
/* */
/* 6.31: Fixed error in AppendDribble for older */
/* compilers not allowing variable definition */
/* within for statement. */
/* */
/* Fixed line count issue when using Windows */
/* line endings in Unix. */
/* */
/* 6.40: Split from filecom.c */
/* */
/* Fix for the batch* command so that the last */
/* command will execute if there is not a crlf. */
/* */
/*************************************************************/
#include <stdio.h>
#include <string.h>
#include "setup.h"
#include "argacces.h"
#include "commline.h"
#include "cstrcpsr.h"
#include "memalloc.h"
#include "prcdrfun.h"
#include "pprint.h"
#include "prntutil.h"
#include "router.h"
#include "scanner.h"
#include "strngrtr.h"
#include "sysdep.h"
#include "filecom.h"
#include "utility.h"
#include "fileutil.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
#if DEBUGGING_FUNCTIONS
static bool QueryDribbleCallback(Environment *,const char *,void *);
static int ReadDribbleCallback(Environment *,const char *,void *);
static int UnreadDribbleCallback(Environment *,const char *,int,void *);
static void ExitDribbleCallback(Environment *,int,void *);
static void WriteDribbleCallback(Environment *,const char *,const char *,void *);
static void PutcDribbleBuffer(Environment *,int);
#endif
static bool QueryBatchCallback(Environment *,const char *,void *);
static int ReadBatchCallback(Environment *,const char *,void *);
static int UnreadBatchCallback(Environment *,const char *,int,void *);
static void ExitBatchCallback(Environment *,int,void *);
static void AddBatch(Environment *,bool,FILE *,const char *,int,const char *,const char *);
#if DEBUGGING_FUNCTIONS
/****************************************/
/* QueryDribbleCallback: Query callback */
/* for the dribble router. */
/****************************************/
static bool QueryDribbleCallback(
Environment *theEnv,
const char *logicalName,
void *context)
{
#if MAC_XCD
#pragma unused(theEnv,context)
#endif
if ( (strcmp(logicalName,STDOUT) == 0) ||
(strcmp(logicalName,STDIN) == 0) ||
(strcmp(logicalName,STDERR) == 0) ||
(strcmp(logicalName,STDWRN) == 0) )
{ return true; }
return false;
}
/******************/
/* AppendDribble: */
/******************/
void AppendDribble(
Environment *theEnv,
const char *str)
{
int i;
if (! DribbleActive(theEnv)) return;
for (i = 0 ; str[i] != EOS ; i++)
{ PutcDribbleBuffer(theEnv,str[i]); }
}
/****************************************/
/* WriteDribbleCallback: Write callback */
/* for the dribble router. */
/****************************************/
static void WriteDribbleCallback(
Environment *theEnv,
const char *logicalName,
const char *str,
void *context)
{
int i;
/*======================================*/
/* Send the output to the dribble file. */
/*======================================*/
for (i = 0 ; str[i] != EOS ; i++)
{ PutcDribbleBuffer(theEnv,str[i]); }
/*===========================================================*/
/* Send the output to any routers interested in printing it. */
/*===========================================================*/
DeactivateRouter(theEnv,"dribble");
WriteString(theEnv,logicalName,str);
ActivateRouter(theEnv,"dribble");
}
/**************************************/
/* ReadDribbleCallback: Read callback */
/* for the dribble router. */
/**************************************/
static int ReadDribbleCallback(
Environment *theEnv,
const char *logicalName,
void *context)
{
int rv;
/*===========================================*/
/* Deactivate the dribble router and get the */
/* character from another active router. */
/*===========================================*/
DeactivateRouter(theEnv,"dribble");
rv = ReadRouter(theEnv,logicalName);
ActivateRouter(theEnv,"dribble");
/*==========================================*/
/* Put the character retrieved from another */
/* router into the dribble buffer. */
/*==========================================*/
PutcDribbleBuffer(theEnv,rv);
/*=======================*/
/* Return the character. */
/*=======================*/
return(rv);
}
/***********************************************************/
/* PutcDribbleBuffer: Putc routine for the dribble router. */
/***********************************************************/
static void PutcDribbleBuffer(
Environment *theEnv,
int rv)
{
/*===================================================*/
/* Receiving an end-of-file character will cause the */
/* contents of the dribble buffer to be flushed. */
/*===================================================*/
if (rv == EOF)
{
if (FileCommandData(theEnv)->DribbleCurrentPosition > 0)
{
fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer);
FileCommandData(theEnv)->DribbleCurrentPosition = 0;
FileCommandData(theEnv)->DribbleBuffer[0] = EOS;
}
}
/*===========================================================*/
/* If we aren't receiving command input, then the character */
/* just received doesn't need to be placed in the dribble */
/* buffer--It can be written directly to the file. This will */
/* occur for example when the command prompt is being */
/* printed (the AwaitingInput variable will be false because */
/* command input has not been receivied yet). Before writing */
/* the character to the file, the dribble buffer is flushed. */
/*===========================================================*/
else if (RouterData(theEnv)->AwaitingInput == false)
{
if (FileCommandData(theEnv)->DribbleCurrentPosition > 0)
{
fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer);
FileCommandData(theEnv)->DribbleCurrentPosition = 0;
FileCommandData(theEnv)->DribbleBuffer[0] = EOS;
}
fputc(rv,FileCommandData(theEnv)->DribbleFP);
}
/*=====================================================*/
/* Otherwise, add the character to the dribble buffer. */
/*=====================================================*/
else
{
FileCommandData(theEnv)->DribbleBuffer = ExpandStringWithChar(theEnv,rv,FileCommandData(theEnv)->DribbleBuffer,
&FileCommandData(theEnv)->DribbleCurrentPosition,
&FileCommandData(theEnv)->DribbleMaximumPosition,
FileCommandData(theEnv)->DribbleMaximumPosition+BUFFER_SIZE);
}
}
/******************************************/
/* UnreadDribbleCallback: Unread callback */
/* for the dribble router. */
/******************************************/
static int UnreadDribbleCallback(
Environment *theEnv,
const char *logicalName,
int ch,
void *context)
{
int rv;
/*===============================================*/
/* Remove the character from the dribble buffer. */
/*===============================================*/
if (FileCommandData(theEnv)->DribbleCurrentPosition > 0) FileCommandData(theEnv)->DribbleCurrentPosition--;
FileCommandData(theEnv)->DribbleBuffer[FileCommandData(theEnv)->DribbleCurrentPosition] = EOS;
/*=============================================*/
/* Deactivate the dribble router and pass the */
/* ungetc request to the other active routers. */
/*=============================================*/
DeactivateRouter(theEnv,"dribble");
rv = UnreadRouter(theEnv,logicalName,ch);
ActivateRouter(theEnv,"dribble");
/*==========================================*/
/* Return the result of the ungetc request. */
/*==========================================*/
return(rv);
}
/**************************************/
/* ExitDribbleCallback: Exit callback */
/* for the dribble router. */
/**************************************/
static void ExitDribbleCallback(
Environment *theEnv,
int num,
void *context)
{
#if MAC_XCD
#pragma unused(num)
#endif
if (FileCommandData(theEnv)->DribbleCurrentPosition > 0)
{ fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer); }
if (FileCommandData(theEnv)->DribbleFP != NULL) GenClose(theEnv,FileCommandData(theEnv)->DribbleFP);
}
/*********************************/
/* DribbleOn: C access routine */
/* for the dribble-on command. */
/*********************************/
bool DribbleOn(
Environment *theEnv,
const char *fileName)
{
/*==============================*/
/* If a dribble file is already */
/* open, then close it. */
/*==============================*/
if (FileCommandData(theEnv)->DribbleFP != NULL)
{ DribbleOff(theEnv); }
/*========================*/
/* Open the dribble file. */
/*========================*/
FileCommandData(theEnv)->DribbleFP = GenOpen(theEnv,fileName,"w");
if (FileCommandData(theEnv)->DribbleFP == NULL)
{
OpenErrorMessage(theEnv,"dribble-on",fileName);
return false;
}
/*============================*/
/* Create the dribble router. */
/*============================*/
AddRouter(theEnv,"dribble",40,
QueryDribbleCallback,WriteDribbleCallback,
ReadDribbleCallback,UnreadDribbleCallback,
ExitDribbleCallback,NULL);
FileCommandData(theEnv)->DribbleCurrentPosition = 0;
/*================================================*/
/* Call the dribble status function. This is used */
/* by some of the machine specific interfaces to */
/* do things such as changing the wording of menu */
/* items from "Turn Dribble On..." to */
/* "Turn Dribble Off..." */
/*================================================*/
if (FileCommandData(theEnv)->DribbleStatusFunction != NULL)
{ (*FileCommandData(theEnv)->DribbleStatusFunction)(theEnv,true); }
/*=====================================*/
/* Return true to indicate the dribble */
/* file was successfully opened. */
/*=====================================*/
return true;
}
/**********************************************/
/* DribbleActive: Returns true if the dribble */
/* router is active, otherwise false. */
/**********************************************/
bool DribbleActive(
Environment *theEnv)
{
if (FileCommandData(theEnv)->DribbleFP != NULL) return true;
return false;
}
/**********************************/
/* DribbleOff: C access routine */
/* for the dribble-off command. */
/**********************************/
bool DribbleOff(
Environment *theEnv)
{
bool rv = false;
/*================================================*/
/* Call the dribble status function. This is used */
/* by some of the machine specific interfaces to */
/* do things such as changing the wording of menu */
/* items from "Turn Dribble On..." to */
/* "Turn Dribble Off..." */
/*================================================*/
if (FileCommandData(theEnv)->DribbleStatusFunction != NULL)
{ (*FileCommandData(theEnv)->DribbleStatusFunction)(theEnv,false); }
/*=======================================*/
/* Close the dribble file and deactivate */
/* the dribble router. */
/*=======================================*/
if (FileCommandData(theEnv)->DribbleFP != NULL)
{
if (FileCommandData(theEnv)->DribbleCurrentPosition > 0)
{ fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer); }
DeleteRouter(theEnv,"dribble");
if (GenClose(theEnv,FileCommandData(theEnv)->DribbleFP) == 0) rv = true;
}
else
{ rv = true; }
FileCommandData(theEnv)->DribbleFP = NULL;
/*============================================*/
/* Free the space used by the dribble buffer. */
/*============================================*/
if (FileCommandData(theEnv)->DribbleBuffer != NULL)
{
rm(theEnv,FileCommandData(theEnv)->DribbleBuffer,FileCommandData(theEnv)->DribbleMaximumPosition);
FileCommandData(theEnv)->DribbleBuffer = NULL;
}
FileCommandData(theEnv)->DribbleCurrentPosition = 0;
FileCommandData(theEnv)->DribbleMaximumPosition = 0;
/*============================================*/
/* Return true if the dribble file was closed */
/* without error, otherwise return false. */
/*============================================*/
return(rv);
}
#endif /* DEBUGGING_FUNCTIONS */
/**************************************/
/* QueryBatchCallback: Query callback */
/* for the batch router. */
/**************************************/
static bool QueryBatchCallback(
Environment *theEnv,
const char *logicalName,
void *context)
{
#if MAC_XCD
#pragma unused(theEnv)
#endif
if (strcmp(logicalName,STDIN) == 0)
{ return true; }
return false;
}
/************************************/
/* ReadBatchCallback: Read callback */
/* for the batch router. */
/************************************/
static int ReadBatchCallback(
Environment *theEnv,
const char *logicalName,
void *context)
{
return(LLGetcBatch(theEnv,logicalName,false));
}
/***************************************************/
/* LLGetcBatch: Lower level routine for retrieving */
/* a character when a batch file is active. */
/***************************************************/
int LLGetcBatch(
Environment *theEnv,
const char *logicalName,
bool returnOnEOF)
{
int rv = EOF, flag = 1;
/*=================================================*/
/* Get a character until a valid character appears */
/* or no more batch files are left. */
/*=================================================*/
while ((rv == EOF) && (flag == 1))
{
if (FileCommandData(theEnv)->BatchType == FILE_BATCH)
{ rv = getc(FileCommandData(theEnv)->BatchFileSource); }
else
{ rv = ReadRouter(theEnv,FileCommandData(theEnv)->BatchLogicalSource); }
if (rv == EOF)
{
if (FileCommandData(theEnv)->BatchCurrentPosition > 0) WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer);
flag = RemoveBatch(theEnv);
}
}
/*=========================================================*/
/* If the character retrieved is an end-of-file character, */
/* then there are no batch files with character input */
/* remaining. Remove the batch router. */
/*=========================================================*/
if (rv == EOF)
{
if (FileCommandData(theEnv)->BatchCurrentPosition > 0) WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer);
DeleteRouter(theEnv,"batch");
RemoveBatch(theEnv);
if (returnOnEOF == true)
{ return (EOF); }
else
{ return ReadRouter(theEnv,logicalName); }
}
/*========================================*/
/* Add the character to the batch buffer. */
/*========================================*/
if (RouterData(theEnv)->InputUngets == 0)
{
FileCommandData(theEnv)->BatchBuffer = ExpandStringWithChar(theEnv,(char) rv,FileCommandData(theEnv)->BatchBuffer,&FileCommandData(theEnv)->BatchCurrentPosition,
&FileCommandData(theEnv)->BatchMaximumPosition,FileCommandData(theEnv)->BatchMaximumPosition+BUFFER_SIZE);
}
/*======================================*/
/* If a carriage return is encountered, */
/* then flush the batch buffer. */
/*======================================*/
if ((char) rv == '\n')
{
WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer);
FileCommandData(theEnv)->BatchCurrentPosition = 0;
if ((FileCommandData(theEnv)->BatchBuffer != NULL) && (FileCommandData(theEnv)->BatchMaximumPosition > BUFFER_SIZE))
{
rm(theEnv,FileCommandData(theEnv)->BatchBuffer,FileCommandData(theEnv)->BatchMaximumPosition);
FileCommandData(theEnv)->BatchMaximumPosition = 0;
FileCommandData(theEnv)->BatchBuffer = NULL;
}
}
/*=============================*/
/* Increment the line counter. */
/*=============================*/
if ((char) rv == '\n')
{ IncrementLineCount(theEnv); }
/*=====================================================*/
/* Return the character retrieved from the batch file. */
/*=====================================================*/
return(rv);
}
/****************************************/
/* UnreadBatchCallback: Unread callback */
/* for the batch router. */
/****************************************/
static int UnreadBatchCallback(
Environment *theEnv,
const char *logicalName,
int ch,
void *context)
{
#if MAC_XCD
#pragma unused(logicalName)
#endif
if (FileCommandData(theEnv)->BatchCurrentPosition > 0) FileCommandData(theEnv)->BatchCurrentPosition--;
if (FileCommandData(theEnv)->BatchBuffer != NULL) FileCommandData(theEnv)->BatchBuffer[FileCommandData(theEnv)->BatchCurrentPosition] = EOS;
if (FileCommandData(theEnv)->BatchType == FILE_BATCH)
{ return ungetc(ch,FileCommandData(theEnv)->BatchFileSource); }
return UnreadRouter(theEnv,FileCommandData(theEnv)->BatchLogicalSource,ch);
}
/************************************/
/* ExitBatchCallback: Exit callback */
/* for the batch router. */
/************************************/
static void ExitBatchCallback(
Environment *theEnv,
int num,
void *context)
{
#if MAC_XCD
#pragma unused(num,context)
#endif
CloseAllBatchSources(theEnv);
}
/**************************************************/
/* Batch: C access routine for the batch command. */
/**************************************************/
bool Batch(
Environment *theEnv,
const char *fileName)
{ return(OpenBatch(theEnv,fileName,false)); }
/***********************************************/
/* OpenBatch: Adds a file to the list of files */
/* opened with the batch command. */
/***********************************************/
bool OpenBatch(
Environment *theEnv,
const char *fileName,
bool placeAtEnd)
{
FILE *theFile;
/*======================*/
/* Open the batch file. */
/*======================*/
theFile = GenOpen(theEnv,fileName,"r");
if (theFile == NULL)
{
OpenErrorMessage(theEnv,"batch",fileName);
return false;
}
/*============================*/
/* Create the batch router if */
/* it doesn't already exist. */
/*============================*/
if (FileCommandData(theEnv)->TopOfBatchList == NULL)
{
AddRouter(theEnv,"batch",20,QueryBatchCallback,NULL,
ReadBatchCallback,UnreadBatchCallback,
ExitBatchCallback,NULL);
}
/*===============================================================*/
/* If a batch file is already open, save its current line count. */
/*===============================================================*/
if (FileCommandData(theEnv)->TopOfBatchList != NULL)
{ FileCommandData(theEnv)->TopOfBatchList->lineNumber = GetLineCount(theEnv); }
#if (! RUN_TIME) && (! BLOAD_ONLY)
/*========================================================================*/
/* If this is the first batch file, remember the prior parsing file name. */
/*========================================================================*/
if (FileCommandData(theEnv)->TopOfBatchList == NULL)
{ FileCommandData(theEnv)->batchPriorParsingFile = CopyString(theEnv,GetParsingFileName(theEnv)); }
/*=======================================================*/
/* Create the error capture router if it does not exist. */
/*=======================================================*/
SetParsingFileName(theEnv,fileName);
SetLineCount(theEnv,0);
CreateErrorCaptureRouter(theEnv);
#endif
/*====================================*/
/* Add the newly opened batch file to */
/* the list of batch files opened. */
/*====================================*/
AddBatch(theEnv,placeAtEnd,theFile,NULL,FILE_BATCH,NULL,fileName);
/*===================================*/
/* Return true to indicate the batch */
/* file was successfully opened. */
/*===================================*/
return true;
}
/*****************************************************************/
/* OpenStringBatch: Opens a string source for batch processing. */
/* The memory allocated for the argument stringName must be */
/* deallocated by the user. The memory allocated for theString */
/* will be deallocated by the batch routines when batch */
/* processing for the string is completed. */
/*****************************************************************/
bool OpenStringBatch(
Environment *theEnv,
const char *stringName,
const char *theString,
bool placeAtEnd)
{
if (OpenStringSource(theEnv,stringName,theString,0) == false)
{ return false; }
if (FileCommandData(theEnv)->TopOfBatchList == NULL)
{
AddRouter(theEnv,"batch", 20,
QueryBatchCallback,NULL,
ReadBatchCallback,UnreadBatchCallback,
ExitBatchCallback,NULL);
}
AddBatch(theEnv,placeAtEnd,NULL,stringName,STRING_BATCH,theString,NULL);
return true;
}
/*******************************************************/
/* AddBatch: Creates the batch file data structure and */
/* adds it to the list of opened batch files. */
/*******************************************************/
static void AddBatch(
Environment *theEnv,
bool placeAtEnd,
FILE *theFileSource,
const char *theLogicalSource,
int type,
const char *theString,
const char *theFileName)
{
struct batchEntry *bptr;
/*=========================*/
/* Create the batch entry. */
/*=========================*/
bptr = get_struct(theEnv,batchEntry);
bptr->batchType = type;
bptr->fileSource = theFileSource;
bptr->logicalSource = CopyString(theEnv,theLogicalSource);
bptr->theString = theString;
bptr->fileName = CopyString(theEnv,theFileName);
bptr->lineNumber = 0;
bptr->next = NULL;
/*============================*/
/* Add the entry to the list. */
/*============================*/
if (FileCommandData(theEnv)->TopOfBatchList == NULL)
{
FileCommandData(theEnv)->TopOfBatchList = bptr;
FileCommandData(theEnv)->BottomOfBatchList = bptr;
FileCommandData(theEnv)->BatchType = type;
FileCommandData(theEnv)->BatchFileSource = theFileSource;
FileCommandData(theEnv)->BatchLogicalSource = bptr->logicalSource;
FileCommandData(theEnv)->BatchCurrentPosition = 0;
}
else if (placeAtEnd == false)
{
bptr->next = FileCommandData(theEnv)->TopOfBatchList;
FileCommandData(theEnv)->TopOfBatchList = bptr;
FileCommandData(theEnv)->BatchType = type;
FileCommandData(theEnv)->BatchFileSource = theFileSource;
FileCommandData(theEnv)->BatchLogicalSource = bptr->logicalSource;
FileCommandData(theEnv)->BatchCurrentPosition = 0;
}
else
{
FileCommandData(theEnv)->BottomOfBatchList->next = bptr;
FileCommandData(theEnv)->BottomOfBatchList = bptr;
}
}
/******************************************************************/
/* RemoveBatch: Removes the top entry on the list of batch files. */
/******************************************************************/
bool RemoveBatch(
Environment *theEnv)
{
struct batchEntry *bptr;
bool rv, fileBatch = false;
if (FileCommandData(theEnv)->TopOfBatchList == NULL) return false;
/*==================================================*/
/* Close the source from which batch input is read. */
/*==================================================*/
if (FileCommandData(theEnv)->TopOfBatchList->batchType == FILE_BATCH)
{
fileBatch = true;
GenClose(theEnv,FileCommandData(theEnv)->TopOfBatchList->fileSource);
#if (! RUN_TIME) && (! BLOAD_ONLY)
FlushParsingMessages(theEnv);
DeleteErrorCaptureRouter(theEnv);
#endif
}
else
{
CloseStringSource(theEnv,FileCommandData(theEnv)->TopOfBatchList->logicalSource);
rm(theEnv,(void *) FileCommandData(theEnv)->TopOfBatchList->theString,
strlen(FileCommandData(theEnv)->TopOfBatchList->theString) + 1);
}
/*=================================*/
/* Remove the entry from the list. */
/*=================================*/
DeleteString(theEnv,(char *) FileCommandData(theEnv)->TopOfBatchList->fileName);
bptr = FileCommandData(theEnv)->TopOfBatchList;
FileCommandData(theEnv)->TopOfBatchList = FileCommandData(theEnv)->TopOfBatchList->next;
DeleteString(theEnv,(char *) bptr->logicalSource);
rtn_struct(theEnv,batchEntry,bptr);
/*========================================================*/
/* If there are no batch files remaining to be processed, */
/* then free the space used by the batch buffer. */
/*========================================================*/
if (FileCommandData(theEnv)->TopOfBatchList == NULL)
{
FileCommandData(theEnv)->BottomOfBatchList = NULL;
FileCommandData(theEnv)->BatchFileSource = NULL;
FileCommandData(theEnv)->BatchLogicalSource = NULL;
if (FileCommandData(theEnv)->BatchBuffer != NULL)
{
rm(theEnv,FileCommandData(theEnv)->BatchBuffer,FileCommandData(theEnv)->BatchMaximumPosition);
FileCommandData(theEnv)->BatchBuffer = NULL;
}
FileCommandData(theEnv)->BatchCurrentPosition = 0;
FileCommandData(theEnv)->BatchMaximumPosition = 0;
rv = false;
#if (! RUN_TIME) && (! BLOAD_ONLY)
if (fileBatch)
{
SetParsingFileName(theEnv,FileCommandData(theEnv)->batchPriorParsingFile);
DeleteString(theEnv,FileCommandData(theEnv)->batchPriorParsingFile);
FileCommandData(theEnv)->batchPriorParsingFile = NULL;
}
#endif
}
/*===========================================*/
/* Otherwise move on to the next batch file. */
/*===========================================*/
else
{
FileCommandData(theEnv)->BatchType = FileCommandData(theEnv)->TopOfBatchList->batchType;
FileCommandData(theEnv)->BatchFileSource = FileCommandData(theEnv)->TopOfBatchList->fileSource;
FileCommandData(theEnv)->BatchLogicalSource = FileCommandData(theEnv)->TopOfBatchList->logicalSource;
FileCommandData(theEnv)->BatchCurrentPosition = 0;
rv = true;
#if (! RUN_TIME) && (! BLOAD_ONLY)
if (FileCommandData(theEnv)->TopOfBatchList->batchType == FILE_BATCH)
{ SetParsingFileName(theEnv,FileCommandData(theEnv)->TopOfBatchList->fileName); }
SetLineCount(theEnv,FileCommandData(theEnv)->TopOfBatchList->lineNumber);
#endif
}
/*====================================================*/
/* Return true if a batch file if there are remaining */
/* batch files to be processed, otherwise false. */
/*====================================================*/
return(rv);
}
/****************************************/
/* BatchActive: Returns true if a batch */
/* file is open, otherwise false. */
/****************************************/
bool BatchActive(
Environment *theEnv)
{
if (FileCommandData(theEnv)->TopOfBatchList != NULL) return true;
return false;
}
/******************************************************/
/* CloseAllBatchSources: Closes all open batch files. */
/******************************************************/
void CloseAllBatchSources(
Environment *theEnv)
{
/*================================================*/
/* Free the batch buffer if it contains anything. */
/*================================================*/
if (FileCommandData(theEnv)->BatchBuffer != NULL)
{
if (FileCommandData(theEnv)->BatchCurrentPosition > 0) WriteString(theEnv,STDOUT,(char *) FileCommandData(theEnv)->BatchBuffer);
rm(theEnv,FileCommandData(theEnv)->BatchBuffer,FileCommandData(theEnv)->BatchMaximumPosition);
FileCommandData(theEnv)->BatchBuffer = NULL;
FileCommandData(theEnv)->BatchCurrentPosition = 0;
FileCommandData(theEnv)->BatchMaximumPosition = 0;
}
/*==========================*/
/* Delete the batch router. */
/*==========================*/
DeleteRouter(theEnv,"batch");
/*=====================================*/
/* Close each of the open batch files. */
/*=====================================*/
while (RemoveBatch(theEnv))
{ /* Do Nothing */ }
}
#if ! RUN_TIME
/*******************************************************/
/* BatchStar: C access routine for the batch* command. */
/*******************************************************/
bool BatchStar(
Environment *theEnv,
const char *fileName)
{
int inchar;
bool done = false;
FILE *theFile;
char *theString = NULL;
size_t position = 0;
size_t maxChars = 0;
#if (! RUN_TIME) && (! BLOAD_ONLY)
char *oldParsingFileName;
long oldLineCountValue;
#endif
/*======================*/
/* Open the batch file. */
/*======================*/
theFile = GenOpen(theEnv,fileName,"r");
if (theFile == NULL)
{
OpenErrorMessage(theEnv,"batch",fileName);
return false;
}
/*======================================*/
/* Setup for capturing errors/warnings. */
/*======================================*/
#if (! RUN_TIME) && (! BLOAD_ONLY)
oldParsingFileName = CopyString(theEnv,GetParsingFileName(theEnv));
SetParsingFileName(theEnv,fileName);
CreateErrorCaptureRouter(theEnv);
oldLineCountValue = SetLineCount(theEnv,1);
#endif
/*=====================================*/
/* If embedded, clear the error flags. */
/*=====================================*/
if (EvaluationData(theEnv)->CurrentExpression == NULL)
{ ResetErrorFlags(theEnv); }
/*=============================================*/
/* Evaluate commands from the file one by one. */
/*=============================================*/
while (! done)
{
inchar = getc(theFile);
if (inchar == EOF)
{
inchar = '\n';
done = true;
}
theString = ExpandStringWithChar(theEnv,inchar,theString,&position,
&maxChars,maxChars+80);
if (CompleteCommand(theString) != 0)
{
FlushPPBuffer(theEnv);
SetPPBufferStatus(theEnv,false);
RouteCommand(theEnv,theString,false);
FlushPPBuffer(theEnv);
SetHaltExecution(theEnv,false);
SetEvaluationError(theEnv,false);
FlushBindList(theEnv,NULL);
genfree(theEnv,theString,maxChars);
theString = NULL;
maxChars = 0;
position = 0;
#if (! RUN_TIME) && (! BLOAD_ONLY)
FlushParsingMessages(theEnv);
#endif
}
if (inchar == '\n')
{ IncrementLineCount(theEnv); }
}
if (theString != NULL)
{ genfree(theEnv,theString,maxChars); }
/*=======================*/
/* Close the batch file. */
/*=======================*/
GenClose(theEnv,theFile);
/*========================================*/
/* Cleanup for capturing errors/warnings. */
/*========================================*/
#if (! RUN_TIME) && (! BLOAD_ONLY)
FlushParsingMessages(theEnv);
DeleteErrorCaptureRouter(theEnv);
SetLineCount(theEnv,oldLineCountValue);
SetParsingFileName(theEnv,oldParsingFileName);
DeleteString(theEnv,oldParsingFileName);
#endif
return true;
}
#else
/***********************************************/
/* BatchStar: This is the non-functional stub */
/* provided for use with a run-time version. */
/***********************************************/
bool BatchStar(
Environment *theEnv,
const char *fileName)
{
PrintErrorID(theEnv,"FILECOM",1,false);
WriteString(theEnv,STDERR,"Function batch* does not work in run time modules.\n");
return false;
}
#endif