forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
2092 lines (1767 loc) · 63.2 KB
/
io.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 functions responsible for input and output processing.
**
** These provide the concept of a current input and output file. In the
** main module they are opened and closed with the 'OpenInput' and
** 'CloseInput' respectively 'OpenOutput' and 'CloseOutput' calls. All the
** other modules just read from the current input and write to the current
** output file.
**
** This module relies on the functions provided by the operating system
** dependent module 'system.c' for the low level input/output.
*/
#include "io.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "gapstate.h"
#include "gaputils.h"
#include "gvars.h"
#include "listfunc.h"
#include "lists.h"
#include "modules.h"
#include "plist.h"
#include "read.h"
#include "scanner.h"
#include "stringobj.h"
#include "symbols.h"
#include "sysfiles.h"
#include "sysopt.h"
#include "sysstr.h"
#include "trycatch.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#endif
#include <limits.h>
static Char GetLine(TypInputFile * input);
static void PutLine2(TypOutputFile * output, const Char * line, UInt len);
static Obj ReadLineFunc;
static Obj WriteAllFunc;
static Obj IsInputStringStream;
static Obj IsOutputStringStream;
static Obj PositionStream;
static Obj SeekPositionStream;
// 'PrintPromptHook' is a GAP-level variable which can be set to a function
// for printing the GAP prompt. If not bound, 'STATE(Prompt)' is printed.
static Obj PrintPromptHook = 0;
static Obj PrintFormattingStatus;
static Obj SetPrintFormattingStatus;
/****************************************************************************
**
*V FilenameCache . . . . . . . . . . . . . . . . . . list of filenames
**
** 'FilenameCache' is a list of previously opened filenames.
*/
static Obj FilenameCache;
static SymbolTable FilenameSymbolTable;
static ModuleStateOffset IOStateOffset = -1;
enum {
MAX_OPEN_FILES = 16,
};
struct IOModuleState {
// A pointer to the current input file
TypInputFile * Input;
// A pointer to the current output file. It points to the top of the
// stack 'OutputFiles'.
TypOutputFile * Output;
//
TypOutputFile * IgnoreStdoutErrout;
// The file identifier of the current input logfile. If it is not 0 the
// scanner echoes all input from the files '*stdin*' and '*errin*' to
// this file.
TypOutputFile * InputLog;
// The file identifier of the current output logfile. If it is not 0 the
// scanner echoes all output to the files '*stdout*' and '*errout*' to
// this file.
TypOutputFile * OutputLog;
TypOutputFile InputLogFileOrStream;
TypOutputFile OutputLogFileOrStream;
TypOutputFile DefaultOutput;
#ifdef HPCGAP
Obj DefaultOutputStream;
Obj DefaultInputStream;
#endif
Int NoSplitLine;
BOOL PrintFormattingForStdout;
BOOL PrintFormattingForErrout;
};
// for debugging from GDB / lldb, we mark this as extern inline
extern inline struct IOModuleState * IO(void)
{
return (struct IOModuleState *)StateSlotsAtOffset(IOStateOffset);
}
void LockCurrentOutput(BOOL lock)
{
IO()->IgnoreStdoutErrout = lock ? IO()->Output : NULL;
}
TypInputFile * GetCurrentInput(void)
{
return IO()->Input;
}
/****************************************************************************
**
*F GetNextChar() . . . . . . . . . . . . . . . get the next character, local
**
** 'GetNextChar' returns the next character from the current input file.
*/
Char GetNextChar(TypInputFile * input)
{
input->ptr++;
// handle line continuation, i.e., backslash followed by new line; and
// also the case when we run out of buffered data
while (*input->ptr == '\\' || *input->ptr == 0) {
// if we run out of data, get more, and try again
if (*input->ptr == 0) {
GetLine(input);
continue;
}
// we have seen a backslash; so check now if it starts a
// line continuation, i.e., whether it is followed by a line terminator
if (input->ptr[1] == '\n') {
// LF is the line terminator used in Unix and its relatives
input->ptr += 2;
}
else if (input->ptr[1] == '\r' && input->ptr[2] == '\n') {
// CR+LF is the line terminator used by Windows
input->ptr += 3;
}
else {
// if we see a backlash without a line terminator after it, stop
break;
}
// if we get here, we saw a line continuation; change the prompt to a
// partial prompt from now on
SetPrompt("> ");
}
return *input->ptr;
}
// GET_NEXT_CHAR_NO_LC is like GetNextChar, but does not handle
// line continuations. This is used when skipping to the end of the
// current line, when handling comment lines.
Char GET_NEXT_CHAR_NO_LC(TypInputFile * input)
{
char c = *(++input->ptr);
return c ? c : GetLine(input);
}
Char PEEK_NEXT_CHAR(TypInputFile * input)
{
// store the current character
char c = *input->ptr;
// read next character; this will increment input->ptr and then
// possibly read in new line data, and so even might end up resetting
// input->ptr to point at the start of the line buffer, which is
// equal to Input->line+1
char next = GetNextChar(input);
// push back the previous character: first, return input->ptr to the
// previous position; then, if we detect that GetNextChar read a new
// line, also restore the previous character by placing it in the "push
// back buffer"
GAP_ASSERT(input->ptr > input->line);
input->ptr--;
if (input->ptr == input->line)
*input->ptr = c;
// return the next character
return next;
}
Char PEEK_CURR_CHAR(TypInputFile * input)
{
Char c = *input->ptr;
// if no character is available then get one
if (c == '\0') {
GAP_ASSERT(input->ptr > input->line);
input->ptr--;
c = GetNextChar(input);
}
return c;
}
void SKIP_TO_END_OF_LINE(TypInputFile * input)
{
Char c = *input->ptr;
while (c != '\n' && c != '\r' && c != '\377')
c = GET_NEXT_CHAR_NO_LC(input);
}
Int GetInputLineNumber(TypInputFile * input)
{
return input ? input->number : 0;
}
const Char * GetInputLineBuffer(TypInputFile * input)
{
GAP_ASSERT(input);
// first byte of Input->line is reserved for the pushback buffer, so add 1
return input->line + 1;
}
Int GetInputLinePosition(TypInputFile * input)
{
GAP_ASSERT(input);
return input->ptr - GetInputLineBuffer(input);
}
UInt GetInputFilenameID(TypInputFile * input)
{
return input ? input->gapnameid : 0;
}
static void AddCachedFilename(SymbolTable * symtab, UInt id, Obj name)
{
AssPlist(FilenameCache, id, name);
}
Obj GetCachedFilename(UInt id)
{
return ELM_LIST(FilenameCache, id);
}
/****************************************************************************
**
*F * * * * * * * * * * * open input/output functions * * * * * * * * * * * *
*/
#ifdef HPCGAP
static GVarDescriptor DEFAULT_INPUT_STREAM;
static GVarDescriptor DEFAULT_OUTPUT_STREAM;
static UInt OpenDefaultInput(TypInputFile * input)
{
Obj func, stream;
stream = IO()->DefaultInputStream;
if (stream)
return OpenInputStream(input, stream, FALSE);
func = GVarOptFunction(&DEFAULT_INPUT_STREAM);
if (!func)
return OpenInput(input, "*stdin*");
stream = CALL_0ARGS(func);
if (!stream)
ErrorQuit("DEFAULT_INPUT_STREAM() did not return a stream", 0, 0);
if (IsStringConv(stream))
return OpenInput(input, CONST_CSTR_STRING(stream));
IO()->DefaultInputStream = stream;
return OpenInputStream(input, stream, FALSE);
}
static UInt OpenDefaultOutput(TypOutputFile * output)
{
Obj func, stream;
stream = IO()->DefaultOutputStream;
if (stream)
return OpenOutputStream(output, stream);
func = GVarOptFunction(&DEFAULT_OUTPUT_STREAM);
if (!func)
return OpenOutput(output, "*stdout*", FALSE);
stream = CALL_0ARGS(func);
if (!stream)
ErrorQuit("DEFAULT_OUTPUT_STREAM() did not return a stream", 0, 0);
if (IsStringConv(stream))
return OpenOutput(output, CONST_CSTR_STRING(stream), FALSE);
IO()->DefaultOutputStream = stream;
return OpenOutputStream(output, stream);
}
#endif
/****************************************************************************
**
*F OpenInput( <filename> ) . . . . . . . . . . open a file as current input
**
** 'OpenInput' opens the file with the name <filename> as current input.
** All subsequent input will be taken from that file, until it is closed
** again with 'CloseInput' or another file is opened with 'OpenInput'.
** 'OpenInput' will not close the current file, i.e., if <filename> is
** closed again, input will again be taken from the current input file.
**
** 'OpenInput' returns 1 if it could successfully open <filename> for
** reading and 0 to indicate failure. 'OpenInput' will fail if the file
** does not exist or if you do not have permissions to read it. 'OpenInput'
** may also fail if you have too many files open at once. It is system
** dependent how many are too many, but 16 files should work everywhere.
**
** You can open '*stdin*' to read from the standard input file, which is
** usually the terminal, or '*errin*' to read from the standard error file,
** which is the terminal even if '*stdin*' is redirected from a file.
** 'OpenInput' passes those file names to 'SyFopen' like any other name,
** they are just a convention between the main and the system package.
** 'SyFopen' and thus 'OpenInput' will fail to open '*errin*' if the file
** 'stderr' (Unix file descriptor 2) is not a terminal, because of a
** redirection say, to avoid that break loops take their input from a file.
**
** It is not necessary to open the initial input file, 'InitScanner' opens
** '*stdin*' for that purpose. This file on the other hand cannot be
** closed by 'CloseInput'.
*/
UInt OpenInput(TypInputFile * input, const Char * filename)
{
GAP_ASSERT(input);
Int file;
#ifdef HPCGAP
/* Handle *defin*; redirect *errin* to *defin* if the default
* channel is already open. */
if (streq(filename, "*defin*") ||
(streq(filename, "*errin*") && IO()->DefaultInputStream))
return OpenDefaultInput(input);
#endif
// try to open the input file
file = SyFopen(filename, "r", TRUE);
if ( file == -1 )
return 0;
// enter the file identifier and the file name
#ifdef GAP_KERNEL_DEBUG
// paranoia: fill with garbage, to verify we initialize everything
memset(input, 0x47, sizeof(TypInputFile));
#endif
input->prev = IO()->Input;
input->stream = 0;
input->file = file;
// enable echo for stdin and errin
if (streq("*errin*", filename) || streq("*stdin*", filename))
input->echo = TRUE;
else
input->echo = FALSE;
input->gapnameid = LookupSymbol(&FilenameSymbolTable, filename);
// start with an empty line
input->line[0] = '\0'; // init the pushback buffer
input->line[1] = '\0'; // empty line buffer
input->ptr = input->line + 1;
input->number = 1;
input->lastErrorLine = 0;
IO()->Input = input;
// indicate success
return 1;
}
/****************************************************************************
**
*F OpenInputStream( <stream>, <echo> ) . . . open a stream as current input
**
** The same as 'OpenInput' but for streams.
*/
UInt OpenInputStream(TypInputFile * input, Obj stream, BOOL echo)
{
GAP_ASSERT(input);
// enter the file identifier and the file name
#ifdef GAP_KERNEL_DEBUG
// paranoia: fill with garbage, to verify we initialize everything
memset(input, 0x47, sizeof(TypInputFile));
#endif
input->prev = IO()->Input;
input->stream = stream;
input->file = -1;
input->isstringstream = (CALL_1ARGS(IsInputStringStream, stream) == True);
if (input->isstringstream) {
input->sline = CONST_ADDR_OBJ(stream)[2];
input->spos = INT_INTOBJ(CONST_ADDR_OBJ(stream)[1]);
}
else {
input->sline = 0;
}
input->echo = echo;
input->gapnameid = LookupSymbol(&FilenameSymbolTable, "stream");
// start with an empty line
input->line[0] = '\0'; // init the pushback buffer
input->line[1] = '\0'; // empty line buffer
input->ptr = input->line + 1;
input->number = 1;
input->lastErrorLine = 0;
IO()->Input = input;
// indicate success
return 1;
}
/****************************************************************************
**
*F CloseInput() . . . . . . . . . . . . . . . . . close current input file
**
** 'CloseInput' will close the current input file. Subsequent input will
** again be taken from the previous input file. 'CloseInput' will return 1
** to indicate success.
**
** 'CloseInput' will not close the initial input file '*stdin*', and returns
** 0 if such an attempt is made. This is used in 'Error' which calls
** 'CloseInput' until it returns 0, thereby closing all open input files.
**
** Calling 'CloseInput' if the corresponding 'OpenInput' call failed will
** close the current output file, which will lead to very strange behaviour.
*/
UInt CloseInput(TypInputFile * input)
{
GAP_ASSERT(input);
GAP_ASSERT(input == IO()->Input);
// revert to previous input
IO()->Input = input->prev;
if (input->stream) {
// if the input stream supports seeking, update its position to
// reflect the actual state of things: we may have read and buffered
// more bytes than we actually processed
int offset = strlen(input->ptr);
// check for EOF
if (input->ptr[0] == '\377' && input->ptr[1] == '\0')
offset = 0;
if (offset) {
Obj pos = CALL_1ARGS(PositionStream, input->stream);
C_DIFF_FIA(pos, pos, INTOBJ_INT(offset));
CALL_2ARGS(SeekPositionStream, input->stream, pos);
}
} else {
// close the input file
SyFclose(input->file);
}
// don't keep GAP objects alive unnecessarily
input->stream = 0;
input->sline = 0;
return 1;
}
/****************************************************************************
**
*F FlushRestOfInputLine() . . . . . . . . . . . . discard remainder of line
*/
void FlushRestOfInputLine(TypInputFile * input)
{
input->ptr[0] = input->ptr[1] = '\0';
}
/****************************************************************************
**
*F OpenLog( <filename> ) . . . . . . . . . . . . . log interaction to a file
**
** 'OpenLog' instructs the scanner to echo all input from the files
** '*stdin*' and '*errin*' and all output to the files '*stdout*' and
** '*errout*' to the file with name <filename>. The file is truncated to
** size 0 if it existed, otherwise it is created.
**
** 'OpenLog' returns 1 if it could successfully open <filename> for writing
** and 0 to indicate failure. 'OpenLog' will fail if you do not have
** permissions to create the file or write to it. 'OpenOutput' may also
** fail if you have too many files open at once. It is system dependent how
** many are too many, but 16 files should work everywhere. Finally
** 'OpenLog' will fail if there is already a current logfile.
*/
UInt OpenLog (
const Char * filename )
{
// refuse to open a logfile if we already log to one
if (IO()->InputLog != 0 || IO()->OutputLog != 0)
return 0;
// try to open the file
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w", FALSE);
IO()->OutputLogFileOrStream.stream = 0;
if (IO()->OutputLogFileOrStream.file == -1)
return 0;
IO()->InputLog = &IO()->OutputLogFileOrStream;
IO()->OutputLog = &IO()->OutputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F OpenLogStream( <stream> ) . . . . . . . . . . log interaction to a stream
**
** The same as 'OpenLog' but for streams.
*/
UInt OpenLogStream (
Obj stream )
{
// refuse to open a logfile if we already log to one
if (IO()->InputLog != 0 || IO()->OutputLog != 0)
return 0;
// try to open the file
IO()->OutputLogFileOrStream.stream = stream;
IO()->OutputLogFileOrStream.file = -1;
IO()->InputLog = &IO()->OutputLogFileOrStream;
IO()->OutputLog = &IO()->OutputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F CloseLog() . . . . . . . . . . . . . . . . . . close the current logfile
**
** 'CloseLog' 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. 'CloseLog' will return 1 to indicate success.
**
** 'CloseLog' will fail if there is no logfile active and will return 0 in
** this case.
*/
UInt CloseLog ( void )
{
// refuse to close a non existent logfile
if (IO()->InputLog == 0 || IO()->OutputLog == 0 ||
IO()->InputLog != IO()->OutputLog)
return 0;
// close the logfile
if (!IO()->InputLog->stream) {
SyFclose(IO()->InputLog->file);
}
IO()->InputLog = 0;
IO()->OutputLog = 0;
// indicate success
return 1;
}
/****************************************************************************
**
*F OpenInputLog( <filename> ) . . . . . . . . . . . . . log input to a file
**
** 'OpenInputLog' instructs the scanner to echo all input from the files
** '*stdin*' and '*errin*' to the file with name <filename>. The file is
** truncated to size 0 if it existed, otherwise it is created.
**
** 'OpenInputLog' returns 1 if it could successfully open <filename> for
** writing and 0 to indicate failure. 'OpenInputLog' will fail if you do
** not have permissions to create the file or write to it. 'OpenInputLog'
** may also fail if you have too many files open at once. It is system
** dependent how many are too many, but 16 files should work everywhere.
** Finally 'OpenInputLog' will fail if there is already a current logfile.
*/
UInt OpenInputLog (
const Char * filename )
{
// refuse to open a logfile if we already log to one
if (IO()->InputLog != 0)
return 0;
// try to open the file
IO()->InputLogFileOrStream.file = SyFopen(filename, "w", FALSE);
IO()->InputLogFileOrStream.stream = 0;
if (IO()->InputLogFileOrStream.file == -1)
return 0;
IO()->InputLog = &IO()->InputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F OpenInputLogStream( <stream> ) . . . . . . . . . . log input to a stream
**
** The same as 'OpenInputLog' but for streams.
*/
UInt OpenInputLogStream (
Obj stream )
{
// refuse to open a logfile if we already log to one
if (IO()->InputLog != 0)
return 0;
// try to open the file
IO()->InputLogFileOrStream.stream = stream;
IO()->InputLogFileOrStream.file = -1;
IO()->InputLog = &IO()->InputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F CloseInputLog() . . . . . . . . . . . . . . . . close the current logfile
**
** 'CloseInputLog' closes the current logfile again, so that input from
** '*stdin*' and '*errin*' will no longer be echoed to a file.
** 'CloseInputLog' will return 1 to indicate success.
**
** 'CloseInputLog' will fail if there is no logfile active and will return 0
** in this case.
*/
UInt CloseInputLog ( void )
{
// refuse to close a non existent logfile
if (IO()->InputLog == 0)
return 0;
// refuse to close a log opened with LogTo
if (IO()->InputLog == IO()->OutputLog)
return 0;
// close the logfile
if (!IO()->InputLog->stream) {
SyFclose(IO()->InputLog->file);
}
IO()->InputLog = 0;
// indicate success
return 1;
}
/****************************************************************************
**
*F OpenOutputLog( <filename> ) . . . . . . . . . . . log output to a file
**
** 'OpenInputLog' instructs the scanner to echo all output to the files
** '*stdout*' and '*errout*' to the file with name <filename>. The file is
** truncated to size 0 if it existed, otherwise it is created.
**
** 'OpenOutputLog' returns 1 if it could successfully open <filename> for
** writing and 0 to indicate failure. 'OpenOutputLog' will fail if you do
** not have permissions to create the file or write to it. 'OpenOutputLog'
** may also fail if you have too many files open at once. It is system
** dependent how many are too many, but 16 files should work everywhere.
** Finally 'OpenOutputLog' will fail if there is already a current logfile.
*/
UInt OpenOutputLog (
const Char * filename )
{
// refuse to open a logfile if we already log to one
if (IO()->OutputLog != 0)
return 0;
// try to open the file
memset(&IO()->OutputLogFileOrStream, 0, sizeof(TypOutputFile));
IO()->OutputLogFileOrStream.stream = 0;
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w", FALSE);
if (IO()->OutputLogFileOrStream.file == -1)
return 0;
IO()->OutputLog = &IO()->OutputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F OpenOutputLogStream( <stream> ) . . . . . . . . log output to a stream
**
** The same as 'OpenOutputLog' but for streams.
*/
UInt OpenOutputLogStream (
Obj stream )
{
// refuse to open a logfile if we already log to one
if (IO()->OutputLog != 0)
return 0;
// try to open the file
memset(&IO()->OutputLogFileOrStream, 0, sizeof(TypOutputFile));
IO()->OutputLogFileOrStream.stream = stream;
IO()->OutputLogFileOrStream.file = -1;
IO()->OutputLog = &IO()->OutputLogFileOrStream;
// otherwise indicate success
return 1;
}
/****************************************************************************
**
*F CloseOutputLog() . . . . . . . . . . . . . . . close the current logfile
**
** 'CloseInputLog' closes the current logfile again, so that output to
** '*stdout*' and '*errout*' will no longer be echoed to a file.
** 'CloseOutputLog' will return 1 to indicate success.
**
** 'CloseOutputLog' will fail if there is no logfile active and will return
** 0 in this case.
*/
UInt CloseOutputLog ( void )
{
// refuse to close a non existent logfile
if (IO()->OutputLog == 0)
return 0;
// refuse to close a log opened with LogTo
if (IO()->OutputLog == IO()->InputLog)
return 0;
// close the logfile
if (!IO()->OutputLog->stream) {
SyFclose(IO()->OutputLog->file);
}
IO()->OutputLog = 0;
// indicate success
return 1;
}
/****************************************************************************
**
*F OpenOutput( <filename> ) . . . . . . . . . open a file as current output
**
** 'OpenOutput' opens the file with the name <filename> as current output.
** All subsequent output will go to that file, until either it is closed
** again with 'CloseOutput' or another file is opened with 'OpenOutput'.
** The file is truncated to size 0 if it existed, otherwise it is created.
** 'OpenOutput' does not close the current file, i.e., if <filename> is
** closed again, output will go again to the current output file.
**
** 'OpenOutput' returns 1 if it could successfully open <filename> for
** writing and 0 to indicate failure. 'OpenOutput' will fail if you do not
** have permissions to create the file or write to it. 'OpenOutput' may
** also fail if you have too many files open at once. It is system
** dependent how many are too many, but 16 files should work everywhere.
**
** You can open '*stdout*' to write to the standard output file, which is
** usually the terminal, or '*errout*' to write to the standard error file,
** which is the terminal even if '*stdout*' is redirected to a file.
** 'OpenOutput' passes those file names to 'SyFopen' like any other name,
** they are just a convention between the main and the system package.
**
** The function does nothing and returns success for '*stdout*' and
** '*errout*' when 'LockCurrentOutput(1)' is in effect (used for testing
** purposes).
**
** It is not necessary to open the initial output file; '*stdout'* is
** opened for that purpose during startup. This file on the other hand can
** not be closed by 'CloseOutput'.
**
** If <append> is set to true, then 'OpenOutput' does not truncate the file
** to size 0 if it exists.
*/
UInt OpenOutput(TypOutputFile * output, const Char * filename, BOOL append)
{
GAP_ASSERT(output);
// do nothing for stdout and errout if caught
if (IO()->Output != NULL && IO()->IgnoreStdoutErrout == IO()->Output &&
(streq(filename, "*errout*") || streq(filename, "*stdout*"))) {
return 1;
}
#ifdef HPCGAP
/* Handle *defout* specially; also, redirect *errout* if we already
* have a default channel open. */
if (streq(filename, "*defout*") ||
(streq(filename, "*errout*") && TLS(threadID) != 0))
return OpenDefaultOutput(output);
#endif
// try to open the file
Int file = SyFopen(filename, append ? "a" : "w", FALSE);
if ( file == -1 )
return 0;
// put the file on the stack, start at position 0 on an empty line
#ifdef GAP_KERNEL_DEBUG
// paranoia: fill with garbage, to verify we initialize everything
memset(output, 0x47, sizeof(TypOutputFile));
#endif
output->prev = IO()->Output;
IO()->Output = output;
output->isstringstream = FALSE;
output->stream = 0;
output->file = file;
output->line[0] = '\0';
output->pos = 0;
if (streq(filename, "*stdout*"))
output->format = IO()->PrintFormattingForStdout;
else if (streq(filename, "*errout*"))
output->format = IO()->PrintFormattingForErrout;
else
output->format = TRUE;
output->indent = 0;
// variables related to line splitting, very bad place to split
output->hints[0] = -1;
// indicate success
return 1;
}
/****************************************************************************
**
*F OpenOutputStream( <stream> ) . . . . . . open a stream as current output
**
** The same as 'OpenOutput' (and also 'OpenAppend') but for streams.
*/
UInt OpenOutputStream(TypOutputFile * output, Obj stream)
{
GAP_ASSERT(output);
// put the file on the stack, start at position 0 on an empty line
#ifdef GAP_KERNEL_DEBUG
// paranoia: fill with garbage, to verify we initialize everything
memset(output, 0x47, sizeof(TypOutputFile));
#endif
output->prev = IO()->Output;
IO()->Output = output;
output->isstringstream = (CALL_1ARGS(IsOutputStringStream, stream) == True);
output->stream = stream;
output->file = -1;
output->line[0] = '\0';
output->pos = 0;
output->format = (CALL_1ARGS(PrintFormattingStatus, stream) == True);
output->indent = 0;
// variables related to line splitting, very bad place to split
output->hints[0] = -1;
// indicate success
return 1;
}
/****************************************************************************
**
*F CloseOutput() . . . . . . . . . . . . . . . . . close current output file
**
** 'CloseOutput' will first flush all pending output and then close the
** current output file. Subsequent output will again go to the previous
** output file. 'CloseOutput' returns 1 to indicate success.
**
** 'CloseOutput' will not close the initial output file '*stdout*', and
** returns 0 if such attempt is made. This is used in 'Error' which calls
** 'CloseOutput' until it returns 0, thereby closing all open output files.
**
** Calling 'CloseOutput' if the corresponding 'OpenOutput' call failed will
** close the current output file, which will lead to very strange behaviour.
** On the other hand if you forget to call 'CloseOutput' at the end of a
** 'PrintTo' call or an error will not yield much better results.
*/
UInt CloseOutput(TypOutputFile * output)
{
GAP_ASSERT(output);
// silently refuse to close the test output file; this is probably an
// attempt to close *errout* which is silently not opened, so let's
// silently not close it
if (IO()->IgnoreStdoutErrout == IO()->Output)
return 1;
GAP_ASSERT(output == IO()->Output);
// refuse to close the initial output file '*stdout*'
#ifdef HPCGAP
if (output->prev == 0 && output->stream &&
IO()->DefaultOutputStream == output->stream)
return 0;
#else
if (output->prev == 0)
return 0;
#endif
// flush output and close the file
Pr("%c", (Int)'\03', 0);
if (!output->stream) {
SyFclose(output->file);
}
// revert to previous output file and indicate success
IO()->Output = output->prev;
// don't keep GAP objects alive unnecessarily
output->stream = 0;
return 1;
}
/****************************************************************************
**
*F * * * * * * * * * * * * * * input functions * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F SetPrompt( <prompt> ) . . . . . . . . . . . . . set the user input prompt
*/
void SetPrompt(const char * prompt)
{
if (SyQuiet)
prompt = "";
gap_strlcpy(STATE(Prompt), prompt, sizeof(STATE(Prompt)));
}
/****************************************************************************
**
*F GetLine2( <input>, <buffer>, <length> ) . . . . . . . . get a line, local
*/
static Int GetLine2(TypInputFile * input)
{
Char * buffer = input->line + 1;
UInt length = sizeof(input->line) - 1;
if ( input->stream ) {
if (input->sline == 0 ||
(IS_STRING_REP(input->sline) &&
GET_LEN_STRING(input->sline) <= input->spos)) {
// If we are in the process of quitting, we cannot call
// GAP functions, so we just return EOF.
if (STATE(UserHasQuit) || STATE(UserHasQUIT)) {
input->sline = Fail;
}
else {
input->sline = CALL_1ARGS(ReadLineFunc, input->stream);
}
input->spos = 0;
}
if ( input->sline == Fail || ! IS_STRING(input->sline) ) {
return 0;
}
ConvString(input->sline);