-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathstreams.c
1881 lines (1618 loc) · 52.3 KB
/
streams.c
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
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the various read-eval-print loops and streams related
** stuff. The system depend part is in "sysfiles.c".
*/
#include "streams.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "funcs.h"
#include "gap.h"
#include "gapstate.h"
#include "gaptime.h"
#include "gvars.h"
#include "integer.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#include "precord.h"
#include "read.h"
#include "records.h"
#include "stringobj.h"
#include "sysfiles.h"
#include "sysopt.h"
#include "sysroots.h"
#include "sysstr.h"
#include "trycatch.h"
#include "vars.h"
#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#ifdef HAVE_SELECT
// For FuncUNIXSelect
#include <sys/time.h>
#endif
static Obj IsInputStream;
static Obj IsOutputStream;
#define RequireInputStream(funcname, op) \
RequireArgumentCondition(funcname, op, \
CALL_1ARGS(IsInputStream, op) == True, \
"must be an input stream")
#define RequireOutputStream(funcname, op) \
RequireArgumentCondition(funcname, op, \
CALL_1ARGS(IsOutputStream, op) == True, \
"must be an output stream")
/****************************************************************************
**
*F * * * * * * * * * streams and files related functions * * * * * * * * * *
*/
static UInt OpenInputFileOrStream(const char * funcname,
TypInputFile * input,
Obj inputObj)
{
if (IsStringConv(inputObj)) {
return OpenInput(input, CONST_CSTR_STRING(inputObj));
}
else if (CALL_1ARGS(IsInputStream, inputObj) == True) {
return OpenInputStream(input, inputObj, FALSE);
}
RequireArgumentEx(funcname, inputObj, "<input>",
"must be a string or an input stream");
}
/****************************************************************************
**
*F FuncREAD_ALL_COMMANDS( <self>, <instream>, <echo>, <capture>,
** <resultCallback> )
**
** FuncREAD_ALL_COMMANDS attempts to execute all statements read from the
** stream <instream>. It returns 'fail' if the stream cannot be opened,
** otherwise a list of lists, each entry of which reflects the result of the
** execution of one statement.
**
** If the parameter <echo> is 'true', then the statements are echoed to the
** current output.
**
** If the parameter <capture> is 'true', then any output occurring during
** execution of a statement, including the output of <resultCallback>, is
** captured into a string.
**
** If <resultCallback> is a function, then this function is called on every
** statement result, otherwise this parameter is ignored. Possible outputs of
** this function are captured if <capture> is 'true'.
**
** The results are returned as lists of length at most five, the structure of
** which is explained below:
**
** - The first entry is 'true' if the statement was executed successfully,
** and 'false' otherwise.
**
** - If the first entry is 'true', then the second entry is bound to the
** result of the statement if there was one, and unbound otherwise.
**
** - The third entry is 'true' if the statement ended in a dual semicolon,
** and 'false' otherwise.
**
** - The fourth entry contains the return value of <resultCallback> if
** applicable.
**
** - The fifth entry contains the captured output as a string, if <capture>
** is 'true'.
**
** This function is currently used in interactive tools such as the GAP
** Jupyter kernel to execute cells and is likely to be replaced by a function
** that can read a single command from a stream without losing the rest of
** its content.
*/
Obj READ_ALL_COMMANDS(Obj instream, Obj echo, Obj capture, Obj resultCallback)
{
volatile Obj outstream = 0;
volatile Obj outstreamString = 0;
RequireInputStream("READ_ALL_COMMANDS", instream);
// try to open the streams
TypInputFile input;
if (!OpenInputStream(&input, instream, echo == True)) {
return Fail;
}
if (capture == True) {
outstreamString = NEW_STRING(0);
outstream = DoOperation2Args(ValGVar(GVarName("OutputTextString")),
outstreamString, True);
}
TypOutputFile output;
if (outstream && !OpenOutputStream(&output, outstream)) {
CloseInput(&input);
return Fail;
}
volatile Obj resultList = NEW_PLIST(T_PLIST, 16);
BOOL rethrow = FALSE;
GAP_TRY
{
while (1) {
if (outstream) {
// Clean in case there has been any output
SET_LEN_STRING(outstreamString, 0);
}
BOOL dualSemicolon;
Obj evalResult;
ExecStatus status = ReadEvalCommand(0, &input, &evalResult, &dualSemicolon);
if (status == STATUS_EOF || status == STATUS_QUIT ||
status == STATUS_QQUIT)
break;
Obj result = NEW_PLIST(T_PLIST, 5);
AssPlist(result, 1, False);
PushPlist(resultList, result);
if (status != STATUS_ERROR) {
AssPlist(result, 1, True);
AssPlist(result, 3, dualSemicolon ? True : False);
if (evalResult) {
AssPlist(result, 2, evalResult);
}
if (evalResult && IS_FUNC(resultCallback) && !dualSemicolon) {
Obj tmp = CALL_1ARGS(resultCallback, evalResult);
AssPlist(result, 4, tmp);
}
}
// Capture output
if (capture == True) {
// Flush output
Pr("\03", 0, 0);
Obj copy = CopyToStringRep(outstreamString);
SET_LEN_STRING(outstreamString, 0);
AssPlist(result, 5, copy);
}
}
}
GAP_CATCH
{
rethrow = TRUE;
}
if (outstream)
CloseOutput(&output);
CloseInput(&input);
if (rethrow)
GAP_THROW();
return resultList;
}
static Obj FuncREAD_ALL_COMMANDS(
Obj self, Obj instream, Obj echo, Obj capture, Obj resultCallback)
{
return READ_ALL_COMMANDS(instream, echo, capture, resultCallback);
}
/*
Returns a list with one or two entries. The first
entry is set to "false" if there was any error
executing the command, and "true" otherwise.
The second entry, if present, is the return value of
the command. If it not present, the command returned nothing.
*/
static Obj FuncREAD_COMMAND_REAL(Obj self, Obj stream, Obj echo)
{
Obj result;
Obj evalResult;
RequireInputStream(SELF_NAME, stream);
result = NEW_PLIST(T_PLIST, 2);
AssPlist(result, 1, False);
// open the stream, read a command, and close it again
TypInputFile input;
if (!OpenInputStream(&input, stream, echo == True)) {
return result;
}
ExecStatus status;
GAP_TRY
{
status = ReadEvalCommand(0, &input, &evalResult, 0);
}
GAP_CATCH
{
CloseInput(&input);
GAP_THROW();
}
CloseInput(&input);
if (status == STATUS_EOF || status == STATUS_QQUIT)
return result;
else if (STATE(UserHasQuit) || STATE(UserHasQUIT))
return result;
else if (status == STATUS_RETURN)
Pr("'return' must not be used in file read-eval loop\n", 0, 0);
AssPlist(result, 1, True);
if (evalResult) {
AssPlist(result, 2, evalResult);
}
return result;
}
/****************************************************************************
**
*F READ() . . . . . . . . . . . . . . . . . . . . . . . read current input
**
** Read the current input and close the input stream.
*/
static UInt LastReadValueGVar;
static void READ_INNER(TypInputFile * input)
{
if (STATE(UserHasQuit))
{
Pr("Warning: Entering READ with UserHasQuit set, this should never happen, resetting",0,0);
STATE(UserHasQuit) = FALSE;
}
if (STATE(UserHasQUIT))
{
Pr("Warning: Entering READ with UserHasQUIT set, this should never happen, resetting",0,0);
STATE(UserHasQUIT) = FALSE;
}
AssGVarWithoutReadOnlyCheck( LastReadValueGVar, 0);
// now do the reading
while ( 1 ) {
Obj evalResult;
ExecStatus status = ReadEvalCommand(0, input, &evalResult, 0);
if (STATE(UserHasQuit) || STATE(UserHasQUIT))
break;
// handle return-value or return-void command
if (status == STATUS_RETURN) {
Pr("'return' must not be used in file read-eval loop\n", 0, 0);
}
// handle quit command or <end-of-file>
else if (status == STATUS_EOF || status == STATUS_ERROR)
break;
else if (status == STATUS_QUIT) {
STATE(UserHasQuit) = TRUE;
break;
}
else if (status == STATUS_QQUIT) {
STATE(UserHasQUIT) = TRUE;
break;
}
if (evalResult)
{
AssGVarWithoutReadOnlyCheck( LastReadValueGVar, evalResult);
}
}
}
/****************************************************************************
**
*F READ_AS_FUNC() . . . . . . . . . . . . . read current input as function
**
** Read the current input as function and close the input stream.
*/
Obj READ_AS_FUNC(TypInputFile * input)
{
// now do the reading
Obj evalResult;
ExecStatus status = ReadEvalFile(input, &evalResult);
// get the function
Obj func = (status == STATUS_END) ? evalResult : Fail;
// return the function
return func;
}
/****************************************************************************
**
*F READ_GAP_ROOT( <filename> ) . . . read from gap root, dyn-load or static
**
** 'READ_GAP_ROOT' tries to find a file under the root directory, it will
** search all directories given in 'SyGapRootPaths', check dynamically
** loadable modules and statically linked modules.
*/
Int READ_GAP_ROOT ( const Char * filename )
{
// try to find the GAP file
Obj path = SyFindGapRootFile(filename);
// try to find compiled version of the GAP file
if (SyUseModule) {
// This code section covers transparently loading GAC compiled
// versions of GAP source files, by running code similar to that in
// FuncLOAD_STAT. For example, lib/oper1.g is compiled into C code;
// when reading lib/oper1.g, we instead load its compiled version.
Char module[GAP_PATH_MAX];
strxcpy(module, "GAPROOT/", sizeof(module));
strxcat(module, filename, sizeof(module));
// search for a statically linked module matching the given filename
StructInitInfo * info = LookupStaticModule(module);
if (info) {
// found a matching statically linked module; if there is also
// a GAP file, compare their CRC
if (path && info->crc != SyGAPCRC(CSTR_STRING(path))) {
Pr("#W Static module %s has CRC mismatch, ignoring\n",
(Int)filename, 0);
}
else {
if (SyDebugLoading) {
Pr("#I READ_GAP_ROOT: loading '%s' statically\n",
(Int)filename, 0);
}
ActivateModule(info);
RecordLoadedModule(info, 1, filename);
return 1;
}
}
}
// not found?
if (path == 0)
return 0;
#ifdef GAP_ENABLE_SAVELOAD
// special handling case if we are trying to load compiled modules needed
// for a saved workspace
if (SyRestoring) {
// ErrorQuit is not available
Pr("Can't find compiled module '%s' needed by saved workspace\n",
(Int)filename, 0);
return 0;
}
#endif
// ordinary gap file
if (SyDebugLoading) {
Pr("#I READ_GAP_ROOT: loading '%s' as GAP file\n", (Int)filename, 0);
}
TypInputFile input;
if (!OpenInput(&input, CSTR_STRING(path)))
return 0;
GAP_TRY
{
while (1) {
ExecStatus status = ReadEvalCommand(0, &input, 0, 0);
if (STATE(UserHasQuit) || STATE(UserHasQUIT))
break;
if (status == STATUS_RETURN) {
Pr("'return' must not be used in file", 0, 0);
}
else if (status == STATUS_EOF || status == STATUS_QUIT) {
break;
}
}
}
GAP_CATCH
{
CloseInput(&input);
GAP_THROW();
}
CloseInput(&input);
return 1;
}
/****************************************************************************
**
*F FuncCALL_WITH_STREAM( <stream>, <func>, <args> )
**
** Temporarily set the active output stream to <stream>, then call the
** function <func> with the arguments in the list <args>. This can for
** example be used to capture the output of a function into a string.
*/
static Obj FuncCALL_WITH_STREAM(Obj self, Obj stream, Obj func, Obj args)
{
RequireOutputStream(SELF_NAME, stream);
RequireSmallList(SELF_NAME, args);
TypOutputFile output;
if (!OpenOutputStream(&output, stream)) {
ErrorQuit("CALL_WITH_STREAM: cannot open stream for output", 0, 0);
}
Obj result;
GAP_TRY
{
result = CallFuncList(func, args);
}
GAP_CATCH
{
CloseOutput(&output);
GAP_THROW();
}
if (!CloseOutput(&output)) {
ErrorQuit("CALL_WITH_STREAM: cannot close output", 0, 0);
}
return result;
}
/****************************************************************************
**
*F FuncCLOSE_LOG_TO() . . . . . . . . . . . . . . . . . . . . stop logging
**
** 'FuncCLOSE_LOG_TO' implements a method for 'LogTo'.
**
** 'LogTo()'
**
** 'LogTo' called with no argument closes the current logfile again, so that
** input from '*stdin*' and '*errin*' and output to '*stdout*' and
** '*errout*' will no longer be echoed to a file.
*/
static Obj FuncCLOSE_LOG_TO(Obj self)
{
if ( ! CloseLog() ) {
ErrorQuit("LogTo: cannot close the logfile", 0, 0);
}
return True;
}
/****************************************************************************
**
*F FuncLOG_TO( <filename> ) . . . . . . . . . . . . start logging to a file
**
** 'FuncLOG_TO' implements a method for 'LogTo'
**
** 'LogTo( <filename> )'
**
** 'LogTo' instructs GAP to echo all input from the standard input files,
** '*stdin*' and '*errin*' and all output to the standard output files,
** '*stdout*' and '*errout*', to the file with the name <filename>.
** The file is created if it does not exist, otherwise it is truncated.
*/
static Obj FuncLOG_TO(Obj self, Obj filename)
{
RequireStringRep(SELF_NAME, filename);
if ( ! OpenLog( CONST_CSTR_STRING(filename) ) ) {
ErrorReturnVoid("LogTo: cannot log to %g", (Int)filename, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncLOG_TO_STREAM( <stream> ) . . . . . . . . . start logging to a stream
*/
static Obj FuncLOG_TO_STREAM(Obj self, Obj stream)
{
RequireOutputStream(SELF_NAME, stream);
if ( ! OpenLogStream(stream) ) {
ErrorReturnVoid("LogTo: cannot log to stream", 0, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncCLOSE_INPUT_LOG_TO() . . . . . . . . . . . . . . . . . stop logging
**
** 'FuncCLOSE_INPUT_LOG_TO' implements a method for 'InputLogTo'.
**
** 'InputLogTo()'
**
** 'InputLogTo' called with no argument closes the current logfile again, so
** that input from '*stdin*' and '*errin*' will no longer be echoed to a
** file.
*/
static Obj FuncCLOSE_INPUT_LOG_TO(Obj self)
{
if ( ! CloseInputLog() ) {
ErrorQuit("InputLogTo: cannot close the logfile", 0, 0);
}
return True;
}
/****************************************************************************
**
*F FuncINPUT_LOG_TO( <filename> ) . . . . . . . . . start logging to a file
**
** 'FuncINPUT_LOG_TO' implements a method for 'InputLogTo'
**
** 'InputLogTo( <filename> )'
**
** 'InputLogTo' instructs GAP to echo all input from the standard input
** files, '*stdin*' and '*errin*' to the file with the name <filename>. The
** file is created if it does not exist, otherwise it is truncated.
*/
static Obj FuncINPUT_LOG_TO(Obj self, Obj filename)
{
RequireStringRep(SELF_NAME, filename);
if ( ! OpenInputLog( CONST_CSTR_STRING(filename) ) ) {
ErrorReturnVoid("InputLogTo: cannot log to %g", (Int)filename, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncINPUT_LOG_TO_STREAM( <stream> ) . . . . . . start logging to a stream
*/
static Obj FuncINPUT_LOG_TO_STREAM(Obj self, Obj stream)
{
RequireOutputStream(SELF_NAME, stream);
if ( ! OpenInputLogStream(stream) ) {
ErrorReturnVoid("InputLogTo: cannot log to stream", 0, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncCLOSE_OUTPUT_LOG_TO() . . . . . . . . . . . . . . . . . stop logging
**
** 'FuncCLOSE_OUTPUT_LOG_TO' implements a method for 'OutputLogTo'.
**
** 'OutputLogTo()'
**
** 'OutputLogTo' called with no argument closes the current logfile again,
** so that output from '*stdin*' and '*errin*' will no longer be echoed to a
** file.
*/
static Obj FuncCLOSE_OUTPUT_LOG_TO(Obj self)
{
if ( ! CloseOutputLog() ) {
ErrorQuit("OutputLogTo: cannot close the logfile", 0, 0);
}
return True;
}
/****************************************************************************
**
*F FuncOUTPUT_LOG_TO( <filename> ) . . . . . . . . start logging to a file
**
** 'FuncOUTPUT_LOG_TO' implements a method for 'OutputLogTo'
**
** 'OutputLogTo( <filename> )'
**
** 'OutputLogTo' instructs GAP to echo all output from the standard output
** files, '*stdin*' and '*errin*' to the file with the name <filename>. The
** file is created if it does not exist, otherwise it is truncated.
*/
static Obj FuncOUTPUT_LOG_TO(Obj self, Obj filename)
{
RequireStringRep(SELF_NAME, filename);
if ( ! OpenOutputLog( CONST_CSTR_STRING(filename) ) ) {
ErrorReturnVoid("OutputLogTo: cannot log to %g", (Int)filename, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncOUTPUT_LOG_TO_STREAM( <stream> ) . . . . . start logging to a stream
*/
static Obj FuncOUTPUT_LOG_TO_STREAM(Obj self, Obj stream)
{
RequireOutputStream(SELF_NAME, stream);
if ( ! OpenOutputLogStream(stream) ) {
ErrorReturnVoid("OutputLogTo: cannot log to stream", 0, 0,
"you can 'return;'");
return False;
}
return True;
}
/****************************************************************************
**
*F FuncPrint( <self>, <args> ) . . . . . . . . . . . . . . . . print <args>
*/
static Obj FuncPrint(Obj self, Obj args)
{
volatile Obj arg;
volatile UInt i;
// print all the arguments, take care of strings and functions
for ( i = 1; i <= LEN_PLIST(args); i++ ) {
arg = ELM_LIST(args,i);
if ( IS_PLIST(arg) && 0 < LEN_PLIST(arg) && IsStringConv(arg) ) {
PrintString1(arg);
}
else if ( IS_STRING_REP(arg) ) {
PrintString1(arg);
}
else {
PrintObj( arg );
}
}
return 0;
}
static Obj PRINT_OR_APPEND_TO_FILE_OR_STREAM(Obj args, int append, int file)
{
const char * volatile funcname = append ? "AppendTo" : "PrintTo";
volatile Obj arg;
volatile Obj destination;
volatile UInt i;
// first entry is the file or stream
destination = ELM_LIST(args, 1);
TypOutputFile output;
// try to open the output and handle failures
if (file) {
RequireStringRep(funcname, destination);
i = OpenOutput(&output, CONST_CSTR_STRING(destination), append);
if (!i) {
if (streq(CSTR_STRING(destination), "*errout*")) {
Panic("Failed to open *errout*!");
}
ErrorQuit("%s: cannot open '%g' for output", (Int)funcname,
(Int)destination);
}
}
else {
if (CALL_1ARGS(IsOutputStream, destination) != True) {
ErrorQuit("%s: <outstream> must be an output stream",
(Int)funcname, 0);
}
i = OpenOutputStream(&output, destination);
if (!i) {
ErrorQuit("%s: cannot open stream for output", (Int)funcname, 0);
}
}
// print all the arguments, take care of strings and functions
for ( i = 2; i <= LEN_PLIST(args); i++ ) {
arg = ELM_LIST(args,i);
// if an error occurs stop printing
GAP_TRY
{
if (IS_PLIST(arg) && 0 < LEN_PLIST(arg) && IsStringConv(arg)) {
PrintString1(arg);
}
else if (IS_STRING_REP(arg)) {
PrintString1(arg);
}
else {
PrintObj(arg);
}
}
GAP_CATCH
{
CloseOutput(&output);
GAP_THROW();
}
}
// close the output file again, and return nothing
if (!CloseOutput(&output)) {
ErrorQuit("%s: cannot close output", (Int)funcname, 0);
}
return 0;
}
static Obj PRINT_OR_APPEND_TO(Obj args, int append)
{
return PRINT_OR_APPEND_TO_FILE_OR_STREAM(args, append, 1);
}
static Obj PRINT_OR_APPEND_TO_STREAM(Obj args, int append)
{
return PRINT_OR_APPEND_TO_FILE_OR_STREAM(args, append, 0);
}
/****************************************************************************
**
*F FuncPRINT_TO( <self>, <args> ) . . . . . . . . . . . . . . print <args>
*/
static Obj FuncPRINT_TO(Obj self, Obj args)
{
return PRINT_OR_APPEND_TO(args, 0);
}
/****************************************************************************
**
*F FuncPRINT_TO_STREAM( <self>, <args> ) . . . . . . . . . . . print <args>
*/
static Obj FuncPRINT_TO_STREAM(Obj self, Obj args)
{
/* Note that FuncPRINT_TO_STREAM and FuncAPPEND_TO_STREAM do exactly the
same, they only differ in the function name they print as part
of their error messages. */
return PRINT_OR_APPEND_TO_STREAM(args, 0);
}
/****************************************************************************
**
*F FuncAPPEND_TO( <self>, <args> ) . . . . . . . . . . . . . . append <args>
*/
static Obj FuncAPPEND_TO(Obj self, Obj args)
{
return PRINT_OR_APPEND_TO(args, 1);
}
/****************************************************************************
**
*F FuncAPPEND_TO_STREAM( <self>, <args> ) . . . . . . . . . . append <args>
*/
static Obj FuncAPPEND_TO_STREAM(Obj self, Obj args)
{
/* Note that FuncPRINT_TO_STREAM and FuncAPPEND_TO_STREAM do exactly the
same, they only differ in the function name they print as part
of their error messages. */
return PRINT_OR_APPEND_TO_STREAM(args, 1);
}
/****************************************************************************
**
*F FuncREAD( <self>, <input> ) . . . . . . . . . . . read a file or stream
**
** Read the current input and close the input stream.
*/
static Obj FuncREAD(Obj self, Obj inputObj)
{
TypInputFile input;
if (!OpenInputFileOrStream(SELF_NAME, &input, inputObj))
return False;
GAP_TRY
{
// read the file
READ_INNER(&input);
}
GAP_CATCH
{
CloseInput(&input);
GAP_THROW();
}
if (!CloseInput(&input)) {
ErrorQuit("Panic: READ cannot close input", 0, 0);
}
return True;
}
/****************************************************************************
**
*F FuncREAD_STREAM_LOOP( <self>, <instream>, <outstream> ) . . read a stream
**
** Read data from <instream> in a read-eval-view loop and write all output
** to <outstream>. This is used by the GAP function `RunTests` and hence
** indirectly for implementing `Test` and `TestDirectory`,
*/
static Obj FuncREAD_STREAM_LOOP(Obj self,
Obj instream,
Obj outstream,
Obj ctx)
{
Int res;
volatile Obj context = ctx;
RequireInputStream(SELF_NAME, instream);
RequireOutputStream(SELF_NAME, outstream);
if (context == False)
context = 0;
else if (!IS_LVARS_OR_HVARS(context))
RequireArgument(SELF_NAME, context,
"must be a local variables bag "
"or the value 'false'");
TypInputFile input;
if (!OpenInputStream(&input, instream, FALSE)) {
return False;
}
TypOutputFile output;
if (!OpenOutputStream(&output, outstream)) {
res = CloseInput(&input);
GAP_ASSERT(res);
return False;
}
LockCurrentOutput(TRUE);
// save the old print state
volatile UInt oldPrintObjState = SetPrintObjState(0);
BOOL rethrow = FALSE;
GAP_TRY
{
// now do the reading
while (1) {
Obj evalResult;
BOOL dualSemicolon;
UInt oldtime = SyTime();
// read and evaluate the command
SetPrintObjState(0);
ExecStatus status =
ReadEvalCommand(context, &input, &evalResult, &dualSemicolon);
// stop the stopwatch
UpdateTime(oldtime);
// handle ordinary command
if (status == STATUS_END && evalResult != 0) {
UpdateLast(evalResult);
if (!dualSemicolon) {
ViewObjHandler(evalResult);
}
}
// handle return-value or return-void command
else if (status == STATUS_RETURN) {
Pr("'return' must not be used in file read-eval loop\n", 0, 0);
}
// handle quit command or <end-of-file>
else if (status == STATUS_EOF || status == STATUS_QUIT ||
status == STATUS_QQUIT) {
break;
}
}
}
GAP_CATCH
{
rethrow = TRUE;
}
SetPrintObjState(oldPrintObjState);
LockCurrentOutput(FALSE);
res = CloseInput(&input);
res &= CloseOutput(&output);
if (rethrow)
GAP_THROW();
return res ? True : False;
}
/****************************************************************************
**
*F FuncREAD_AS_FUNC( <self>, <input> ) . read a file or stream as a function
*/
static Obj FuncREAD_AS_FUNC(Obj self, Obj inputObj)
{
TypInputFile input;
if (!OpenInputFileOrStream(SELF_NAME, &input, inputObj))
return False;
Obj func;
GAP_TRY
{
func = READ_AS_FUNC(&input);
}
GAP_CATCH
{
CloseInput(&input);
GAP_THROW();
}
if (!CloseInput(&input)) {
ErrorQuit("Panic: READ_AS_FUNC cannot close input", 0, 0);
}
return func;
}
/****************************************************************************
**
*F FuncREAD_GAP_ROOT( <self>, <filename> ) . . . . . . . . . . . read a file
*/
static Obj FuncREAD_GAP_ROOT(Obj self, Obj filename)
{
Char filenamecpy[GAP_PATH_MAX];
RequireStringRep(SELF_NAME, filename);
// Copy to avoid garbage collection moving string
gap_strlcpy(filenamecpy, CONST_CSTR_STRING(filename), GAP_PATH_MAX);
// try to open the file
return READ_GAP_ROOT(filenamecpy) ? True : False;
}
/****************************************************************************
**
*F FuncTmpName( <self> ) . . . . . . . . . . . . . . return a temporary name
*/
static Obj FuncTmpName(Obj self)
{
char name[100] = "/tmp/gaptempfile.XXXXXX";
#ifdef SYS_IS_CYGWIN32
// If /tmp is missing, write into Window's temp directory
DIR* dir = opendir("/tmp");
if(dir) {
closedir(dir);
}