Debian Codegraph @

gcc/m2/gm2-libs/FIO.mod

raw
1 (* FIO.mod provides a simple buffered file input/output library.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE FIO ;
28
29 (*
30 Title : FIO
31 Author : Gaius Mulley
32 System : UNIX (gm2)
33 Date : Thu Sep 2 22:07:21 1999
34 Last edit : Thu Sep 2 22:07:21 1999
35 Description: a complete reimplememtation of FIO.mod
36 provides a simple buffered file input/output library.
37 *)
38
39 FROM SYSTEM IMPORT ADR, TSIZE, WORD, COFF_T ;
40 FROM ASCII IMPORT nl, nul, tab ;
41 FROM StrLib IMPORT StrLen, StrConCat, StrCopy ;
42 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
43 FROM NumberIO IMPORT CardToStr ;
44 FROM Indexing IMPORT Index, InitIndex, InBounds, HighIndice, PutIndice, GetIndice ;
45 FROM M2RTS IMPORT InstallTerminationProcedure ;
46 FROM libc IMPORT exit, open, creat, read, write, close, lseek, strncpy, memcpy ;
47 FROM wrapc IMPORT SeekSet, SeekEnd, ReadOnly, WriteOnly ;
48
49
50 CONST
51 MaxBufferLength = 1024*16 ;
52 MaxErrorString = 1024* 8 ;
53 CreatePermissions = 666B;
54
55 TYPE
56 FileUsage = (unused, openedforread, openedforwrite, openedforrandom) ;
57 FileStatus = (successful, outofmemory, toomanyfilesopen, failed, connectionfailure, endofline, endoffile) ;
58
59 NameInfo = RECORD
60 address: ADDRESS ;
61 size : CARDINAL ;
62 END ;
63
64 Buffer = POINTER TO buf ;
65 buf = RECORD
66 valid : BOOLEAN ; (* are the field valid? *)
67 bufstart: LONGINT ; (* the position of buffer in file *)
68 position: CARDINAL ; (* where are we through this buffer *)
69 address : ADDRESS ; (* dynamic buffer address *)
70 filled : CARDINAL ; (* length of the buffer filled *)
71 size : CARDINAL ; (* maximum space in this buffer *)
72 left : CARDINAL ; (* number of bytes left to read *)
73 contents: POINTER TO ARRAY [0..MaxBufferLength] OF CHAR ;
74 END ;
75
76 FileDescriptor = POINTER TO fds ;
77 fds = RECORD
78 unixfd: INTEGER ;
79 name : NameInfo ;
80 state : FileStatus ;
81 usage : FileUsage ;
82 output: BOOLEAN ; (* is this file going to write data *)
83 buffer: Buffer ;
84 abspos: LONGINT ; (* absolute position into file. *)
85 END ; (* reflects low level reads which *)
86 (* means this value will normally *)
87 (* be further through the file than *)
88 (* bufstart above. *)
89 PtrToChar = POINTER TO CHAR ;
90
91
92 VAR
93 FileInfo: Index ;
94 Error : File ; (* not stderr, this is an unused file handle
95 which only serves to hold status values
96 when we cannot create a new file handle *)
97
98
99 (*
100 GetUnixFileDescriptor - returns the UNIX file descriptor of a file.
101 *)
102
103 PROCEDURE GetUnixFileDescriptor (f: File) : INTEGER ;
104 VAR
105 fd: FileDescriptor ;
106 BEGIN
107 IF f#Error
108 THEN
109 fd := GetIndice(FileInfo, f) ;
110 IF fd#NIL
111 THEN
112 RETURN( fd^.unixfd )
113 END
114 END ;
115 FormatError1('file %d has not been opened or is out of range\n', f) ;
116 RETURN( -1 )
117 END GetUnixFileDescriptor ;
118
119
120 (*
121 WriteString - writes a string to file, f.
122 *)
123
124 PROCEDURE WriteString (f: File; a: ARRAY OF CHAR) ;
125 VAR
126 l: CARDINAL ;
127 BEGIN
128 l := StrLen(a) ;
129 IF WriteNBytes(f, l, ADR(a))#l
130 THEN
131 END
132 END WriteString ;
133
134
135 (*
136 Max - returns the maximum of two values.
137 *)
138
139 PROCEDURE Max (a, b: CARDINAL) : CARDINAL ;
140 BEGIN
141 IF a>b
142 THEN
143 RETURN( a )
144 ELSE
145 RETURN( b )
146 END
147 END Max ;
148
149
150 (*
151 Min - returns the minimum of two values.
152 *)
153
154 PROCEDURE Min (a, b: CARDINAL) : CARDINAL ;
155 BEGIN
156 IF a<b
157 THEN
158 RETURN( a )
159 ELSE
160 RETURN( b )
161 END
162 END Min ;
163
164
165 (*
166 GetNextFreeDescriptor - returns the index to the FileInfo array indicating
167 the next free slot.
168 *)
169
170 PROCEDURE GetNextFreeDescriptor () : File ;
171 VAR
172 f, h: File ;
173 fd : FileDescriptor ;
174 BEGIN
175 f := Error+1 ;
176 h := HighIndice(FileInfo) ;
177 LOOP
178 IF f<=h
179 THEN
180 fd := GetIndice(FileInfo, f) ;
181 IF fd=NIL
182 THEN
183 RETURN( f )
184 END
185 END ;
186 INC(f) ;
187 IF f>h
188 THEN
189 PutIndice(FileInfo, f, NIL) ; (* create new slot *)
190 RETURN( f )
191 END
192 END
193 END GetNextFreeDescriptor ;
194
195
196 (*
197 IsNoError - returns a TRUE if no error has occured on file, f.
198 *)
199
200 PROCEDURE IsNoError (f: File) : BOOLEAN ;
201 VAR
202 fd: FileDescriptor ;
203 BEGIN
204 IF f=Error
205 THEN
206 RETURN( FALSE )
207 ELSE
208 fd := GetIndice(FileInfo, f) ;
209 RETURN( (fd#NIL) AND ((fd^.state=successful) OR (fd^.state=endoffile) OR (fd^.state=endofline)) )
210 END
211 END IsNoError ;
212
213
214 (*
215 IsActive - returns TRUE if the file, f, is still active.
216 *)
217
218 PROCEDURE IsActive (f: File) : BOOLEAN ;
219 BEGIN
220 IF f=Error
221 THEN
222 RETURN( FALSE )
223 ELSE
224 RETURN( GetIndice(FileInfo, f)#NIL )
225 END
226 END IsActive ;
227
228
229 (*
230 openToRead - attempts to open a file, fname, for reading and
231 it returns this file.
232 The success of this operation can be checked by
233 calling IsNoError.
234 *)
235
236 PROCEDURE openToRead (fname: ADDRESS; flength: CARDINAL) : File ;
237 VAR
238 f: File ;
239 BEGIN
240 f := GetNextFreeDescriptor() ;
241 IF f=Error
242 THEN
243 SetState(f, toomanyfilesopen)
244 ELSE
245 f := InitializeFile(f, fname, flength, successful, openedforread, FALSE, MaxBufferLength) ;
246 ConnectToUnix(f, FALSE, FALSE)
247 END ;
248 RETURN( f )
249 END openToRead ;
250
251
252 (*
253 openToWrite - attempts to open a file, fname, for write and
254 it returns this file.
255 The success of this operation can be checked by
256 calling IsNoError.
257 *)
258
259 PROCEDURE openToWrite (fname: ADDRESS; flength: CARDINAL) : File ;
260 VAR
261 f: File ;
262 BEGIN
263 f := GetNextFreeDescriptor() ;
264 IF f=Error
265 THEN
266 SetState(f, toomanyfilesopen)
267 ELSE
268 f := InitializeFile(f, fname, flength, successful, openedforwrite, TRUE, MaxBufferLength) ;
269 ConnectToUnix(f, TRUE, TRUE)
270 END ;
271 RETURN( f )
272 END openToWrite ;
273
274
275 (*
276 openForRandom - attempts to open a file, fname, for random access
277 read or write and it returns this file.
278 The success of this operation can be checked by
279 calling IsNoError.
280 towrite, determines whether the file should be
281 opened for writing or reading.
282 *)
283
284 PROCEDURE openForRandom (fname: ADDRESS; flength: CARDINAL;
285 towrite, newfile: BOOLEAN) : File ;
286 VAR
287 f: File ;
288 BEGIN
289 f := GetNextFreeDescriptor() ;
290 IF f=Error
291 THEN
292 SetState(f, toomanyfilesopen)
293 ELSE
294 f := InitializeFile(f, fname, flength, successful, openedforrandom, towrite, MaxBufferLength) ;
295 ConnectToUnix(f, towrite, newfile)
296 END ;
297 RETURN( f )
298 END openForRandom ;
299
300
301 (*
302 exists - returns TRUE if a file named, fname exists for reading.
303 *)
304
305 PROCEDURE exists (fname: ADDRESS; flength: CARDINAL) : BOOLEAN ;
306 VAR
307 f: File ;
308 BEGIN
309 f := openToRead(fname, flength) ;
310 IF IsNoError(f)
311 THEN
312 Close(f) ;
313 RETURN( TRUE )
314 ELSE
315 Close(f) ;
316 RETURN( FALSE )
317 END
318 END exists ;
319
320
321 (*
322 SetState - sets the field, state, of file, f, to, s.
323 *)
324
325 PROCEDURE SetState (f: File; s: FileStatus) ;
326 VAR
327 fd: FileDescriptor ;
328 BEGIN
329 fd := GetIndice(FileInfo, f) ;
330 fd^.state := s
331 END SetState ;
332
333
334 (*
335 InitializeFile - initialize a file descriptor
336 *)
337
338 PROCEDURE InitializeFile (f: File; fname: ADDRESS;
339 flength: CARDINAL; fstate: FileStatus;
340 use: FileUsage;
341 towrite: BOOLEAN; buflength: CARDINAL) : File ;
342 VAR
343 p : PtrToChar ;
344 fd: FileDescriptor ;
345 BEGIN
346 NEW(fd) ;
347 IF fd=NIL
348 THEN
349 SetState(Error, outofmemory) ;
350 RETURN( Error )
351 ELSE
352 PutIndice(FileInfo, f, fd) ;
353 WITH fd^ DO
354 name.size := flength+1 ; (* need to guarantee the nul for C *)
355 usage := use ;
356 output := towrite ;
357 ALLOCATE(name.address, name.size) ;
358 IF name.address=NIL
359 THEN
360 state := outofmemory ;
361 RETURN( f )
362 END ;
363 name.address := strncpy(name.address, fname, flength) ;
364 (* and assign nul to the last byte *)
365 p := name.address ;
366 INC(p, flength) ;
367 p^ := nul ;
368 abspos := 0 ;
369 (* now for the buffer *)
370 NEW(buffer) ;
371 IF buffer=NIL
372 THEN
373 SetState(Error, outofmemory) ;
374 RETURN( Error )
375 ELSE
376 WITH buffer^ DO
377 valid := FALSE ;
378 bufstart := 0 ;
379 size := buflength ;
380 position := 0 ;
381 filled := 0 ;
382 IF size=0
383 THEN
384 address := NIL
385 ELSE
386 ALLOCATE(address, size) ;
387 IF address=NIL
388 THEN
389 state := outofmemory ;
390 RETURN( f )
391 END
392 END ;
393 IF towrite
394 THEN
395 left := size
396 ELSE
397 left := 0
398 END ;
399 contents := address ; (* provides easy access for reading characters *)
400 END ;
401 state := fstate
402 END
403 END
404 END ;
405 RETURN( f )
406 END InitializeFile ;
407
408
409 (*
410 ConnectToUnix - connects a FIO file to a UNIX file descriptor.
411 *)
412
413 PROCEDURE ConnectToUnix (f: File; towrite, newfile: BOOLEAN) ;
414 VAR
415 fd: FileDescriptor ;
416 BEGIN
417 IF f#Error
418 THEN
419 fd := GetIndice(FileInfo, f) ;
420 IF fd#NIL
421 THEN
422 WITH fd^ DO
423 IF towrite
424 THEN
425 IF newfile
426 THEN
427 unixfd := creat(name.address, CreatePermissions)
428 ELSE
429 unixfd := open(name.address, INTEGER (WriteOnly ()), 0)
430 END
431 ELSE
432 unixfd := open(name.address, INTEGER (ReadOnly ()), 0)
433 END ;
434 IF unixfd<0
435 THEN
436 state := connectionfailure
437 END
438 END
439 END
440 END
441 END ConnectToUnix ;
442
443
444 (*
445 The following functions are wrappers for the above.
446 *)
447
448 PROCEDURE Exists (fname: ARRAY OF CHAR) : BOOLEAN ;
449 BEGIN
450 RETURN( exists(ADR(fname), StrLen(fname)) )
451 END Exists ;
452
453
454 PROCEDURE OpenToRead (fname: ARRAY OF CHAR) : File ;
455 BEGIN
456 RETURN( openToRead(ADR(fname), StrLen(fname)) )
457 END OpenToRead ;
458
459
460 PROCEDURE OpenToWrite (fname: ARRAY OF CHAR) : File ;
461 BEGIN
462 RETURN( openToWrite(ADR(fname), StrLen(fname)) )
463 END OpenToWrite ;
464
465
466 PROCEDURE OpenForRandom (fname: ARRAY OF CHAR;
467 towrite: BOOLEAN; newfile: BOOLEAN) : File ;
468 BEGIN
469 RETURN( openForRandom(ADR(fname), StrLen(fname), towrite, newfile) )
470 END OpenForRandom ;
471
472
473 (*
474 Close - close a file which has been previously opened using:
475 OpenToRead, OpenToWrite, OpenForRandom.
476 It is correct to close a file which has an error status.
477 *)
478
479 PROCEDURE Close (f: File) ;
480 VAR
481 fd: FileDescriptor ;
482 BEGIN
483 IF f#Error
484 THEN
485 fd := GetIndice(FileInfo, f) ;
486 (*
487 we allow users to close files which have an error status
488 *)
489 IF fd#NIL
490 THEN
491 FlushBuffer(f) ;
492 WITH fd^ DO
493 IF unixfd>=0
494 THEN
495 IF close(unixfd)#0
496 THEN
497 FormatError1('failed to close file (%s)\n', name.address) ;
498 state := failed (* --fixme-- too late to notify user (unless we return a BOOLEAN) *)
499 END
500 END ;
501 IF name.address#NIL
502 THEN
503 DEALLOCATE(name.address, name.size)
504 END ;
505 IF buffer#NIL
506 THEN
507 WITH buffer^ DO
508 IF address#NIL
509 THEN
510 DEALLOCATE(address, size)
511 END
512 END ;
513 DISPOSE(buffer) ;
514 buffer := NIL
515 END
516 END ;
517 DISPOSE(fd) ;
518 PutIndice(FileInfo, f, NIL)
519 END
520 END
521 END Close ;
522
523
524 (*
525 ReadFromBuffer - attempts to read, nBytes, from file, f.
526 It firstly consumes the buffer and then performs
527 direct unbuffered reads. This should only be used
528 when wishing to read large files.
529
530 The actual number of bytes read is returned.
531 -1 is returned if EOF is reached.
532 *)
533
534 PROCEDURE ReadFromBuffer (f: File; a: ADDRESS; nBytes: CARDINAL) : INTEGER ;
535 VAR
536 t : ADDRESS ;
537 result: INTEGER ;
538 total,
539 n : CARDINAL ;
540 p : POINTER TO BYTE ;
541 fd : FileDescriptor ;
542 BEGIN
543 IF f#Error
544 THEN
545 total := 0 ; (* how many bytes have we read *)
546 fd := GetIndice(FileInfo, f) ;
547 WITH fd^ DO
548 (* extract from the buffer first *)
549 IF (buffer#NIL) AND (buffer^.valid)
550 THEN
551 WITH buffer^ DO
552 IF left>0
553 THEN
554 IF nBytes=1
555 THEN
556 (* too expensive to call memcpy for 1 character *)
557 p := a ;
558 p^ := contents^[position] ;
559 DEC(left) ; (* remove consumed bytes *)
560 INC(position) ; (* move onwards n bytes *)
561 nBytes := 0 ; (* reduce the amount for future direct *)
562 (* read *)
563 RETURN( 1 )
564 ELSE
565 n := Min(left, nBytes) ;
566 t := address ;
567 INC(t, position) ;
568 p := memcpy(a, t, n) ;
569 DEC(left, n) ; (* remove consumed bytes *)
570 INC(position, n) ; (* move onwards n bytes *)
571 (* move onwards ready for direct reads *)
572 INC(a, n) ;
573 DEC(nBytes, n) ; (* reduce the amount for future direct *)
574 (* read *)
575 INC(total, n) ;
576 RETURN( total ) (* much cleaner to return now, *)
577 END (* difficult to record an error if *)
578 END (* the read below returns -1 *)
579 END
580 END ;
581 IF nBytes>0
582 THEN
583 (* still more to read *)
584 result := read(unixfd, a, INTEGER(nBytes)) ;
585 IF result>0
586 THEN
587 INC(total, result) ;
588 INC(abspos, result) ;
589 (* now disable the buffer as we read directly into, a. *)
590 IF buffer#NIL
591 THEN
592 buffer^.valid := FALSE
593 END ;
594 ELSE
595 IF result=0
596 THEN
597 (* eof reached *)
598 state := endoffile
599 ELSE
600 state := failed
601 END ;
602 (* indicate buffer is empty *)
603 IF buffer#NIL
604 THEN
605 WITH buffer^ DO
606 valid := FALSE ;
607 left := 0 ;
608 position := 0 ;
609 IF address#NIL
610 THEN
611 contents^[position] := nul
612 END
613 END
614 END ;
615 RETURN( -1 )
616 END
617 END
618 END ;
619 RETURN( total )
620 ELSE
621 RETURN( -1 )
622 END
623 END ReadFromBuffer ;
624
625
626 (*
627 ReadNBytes - reads nBytes of a file into memory area, dest, returning
628 the number of bytes actually read.
629 This function will consume from the buffer and then
630 perform direct libc reads. It is ideal for large reads.
631 *)
632
633 PROCEDURE ReadNBytes (f: File; nBytes: CARDINAL; dest: ADDRESS) : CARDINAL ;
634 VAR
635 n: INTEGER ;
636 p: POINTER TO CHAR ;
637 BEGIN
638 IF f # Error
639 THEN
640 CheckAccess (f, openedforread, FALSE) ;
641 n := ReadFromBuffer (f, dest, nBytes) ;
642 IF n <= 0
643 THEN
644 RETURN 0
645 ELSE
646 p := dest ;
647 INC (p, n-1) ;
648 SetEndOfLine (f, p^) ;
649 RETURN n
650 END
651 ELSE
652 RETURN 0
653 END
654 END ReadNBytes ;
655
656
657 (*
658 BufferedRead - will read, nBytes, through the buffer.
659 Similar to ReadFromBuffer, but this function will always
660 read into the buffer before copying into memory.
661
662 Useful when performing small reads.
663 *)
664
665 PROCEDURE BufferedRead (f: File; nBytes: CARDINAL; dest: ADDRESS) : INTEGER ;
666 VAR
667 src : ADDRESS ;
668 total,
669 n : INTEGER ;
670 p : POINTER TO BYTE ;
671 fd : FileDescriptor ;
672 BEGIN
673 IF f#Error
674 THEN
675 fd := GetIndice (FileInfo, f) ;
676 total := 0 ; (* how many bytes have we read *)
677 IF fd#NIL
678 THEN
679 WITH fd^ DO
680 (* extract from the buffer first *)
681 IF buffer # NIL
682 THEN
683 WITH buffer^ DO
684 WHILE nBytes > 0 DO
685 IF (left > 0) AND valid
686 THEN
687 IF nBytes = 1
688 THEN
689 (* too expensive to call memcpy for 1 character *)
690 p := dest ;
691 p^ := contents^[position] ;
692 DEC (left) ; (* remove consumed byte *)
693 INC (position) ; (* move onwards n byte *)
694 INC (total) ;
695 RETURN( total )
696 ELSE
697 n := Min (left, nBytes) ;
698 src := address ;
699 INC (src, position) ;
700 p := memcpy (dest, src, n) ;
701 DEC (left, n) ; (* remove consumed bytes *)
702 INC (position, n) ; (* move onwards n bytes *)
703 (* move onwards ready for direct reads *)
704 INC (dest, n) ;
705 DEC (nBytes, n) ; (* reduce the amount for future direct *)
706 (* read *)
707 INC (total, n)
708 END
709 ELSE
710 (* refill buffer *)
711 n := read (unixfd, address, size) ;
712 IF n >= 0
713 THEN
714 valid := TRUE ;
715 position := 0 ;
716 left := n ;
717 filled := n ;
718 bufstart := abspos ;
719 INC (abspos, n) ;
720 IF n = 0
721 THEN
722 (* eof reached *)
723 state := endoffile ;
724 RETURN( -1 )
725 END
726 ELSE
727 valid := FALSE ;
728 position := 0 ;
729 left := 0 ;
730 filled := 0 ;
731 state := failed ;
732 RETURN( total )
733 END
734 END
735 END
736 END ;
737 RETURN( total )
738 END
739 END
740 END
741 END ;
742 RETURN( -1 )
743 END BufferedRead ;
744
745
746 (*
747 HandleEscape - translates \n and \t into their respective ascii codes.
748 *)
749
750 PROCEDURE HandleEscape (VAR dest: ARRAY OF CHAR; src: ARRAY OF CHAR;
751 VAR i, j: CARDINAL; HighSrc, HighDest: CARDINAL) ;
752 BEGIN
753 IF (i+1<HighSrc) AND (src[i]='\') AND (j<HighDest)
754 THEN
755 IF src[i+1]='n'
756 THEN
757 (* requires a newline *)
758 dest[j] := nl ;
759 INC(j) ;
760 INC(i, 2)
761 ELSIF src[i+1]='t'
762 THEN
763 (* requires a tab (yuck) tempted to fake this but I better not.. *)
764 dest[j] := tab ;
765 INC(j) ;
766 INC(i, 2)
767 ELSE
768 (* copy escaped character *)
769 INC(i) ;
770 dest[j] := src[i] ;
771 INC(j) ;
772 INC(i)
773 END
774 END
775 END HandleEscape ;
776
777
778 (*
779 Cast - casts a := b
780 *)
781
782 PROCEDURE Cast (VAR a: ARRAY OF BYTE; b: ARRAY OF BYTE) ;
783 VAR
784 i: CARDINAL ;
785 BEGIN
786 IF HIGH(a)=HIGH(b)
787 THEN
788 FOR i := 0 TO HIGH(a) DO
789 a[i] := b[i]
790 END
791 ELSE
792 FormatError('cast failed')
793 END
794 END Cast ;
795
796
797 (*
798 StringFormat1 - converts string, src, into, dest, together with encapsulated
799 entity, w. It only formats the first %s or %d with n.
800 *)
801
802 PROCEDURE StringFormat1 (VAR dest: ARRAY OF CHAR; src: ARRAY OF CHAR;
803 w: ARRAY OF BYTE) ;
804 VAR
805 HighSrc,
806 HighDest,
807 c, i, j : CARDINAL ;
808 str : ARRAY [0..MaxErrorString] OF CHAR ;
809 p : POINTER TO CHAR ;
810 BEGIN
811 HighSrc := StrLen(src) ;
812 HighDest := HIGH(dest) ;
813 p := NIL ;
814 c := 0 ;
815 i := 0 ;
816 j := 0 ;
817 WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) AND (src[i]#'%') DO
818 IF src[i]='\'
819 THEN
820 HandleEscape(dest, src, i, j, HighSrc, HighDest)
821 ELSE
822 dest[j] := src[i] ;
823 INC(i) ;
824 INC(j)
825 END
826 END ;
827
828 IF (i+1<HighSrc) AND (src[i]='%') AND (j<HighDest)
829 THEN
830 IF src[i+1]='s'
831 THEN
832 Cast(p, w) ;
833 WHILE (j<HighDest) AND (p^#nul) DO
834 dest[j] := p^ ;
835 INC(j) ;
836 INC(p)
837 END ;
838 IF j<HighDest
839 THEN
840 dest[j] := nul
841 END ;
842 j := StrLen(dest) ;
843 INC(i, 2)
844 ELSIF src[i+1]='d'
845 THEN
846 dest[j] := nul ;
847 Cast(c, w) ;
848 CardToStr(c, 0, str) ;
849 StrConCat(dest, str, dest) ;
850 j := StrLen(dest) ;
851 INC(i, 2)
852 ELSE
853 dest[j] := src[i] ;
854 INC(i) ;
855 INC(j)
856 END
857 END ;
858 (* and finish off copying src into dest *)
859 WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) DO
860 IF src[i]='\'
861 THEN
862 HandleEscape(dest, src, i, j, HighSrc, HighDest)
863 ELSE
864 dest[j] := src[i] ;
865 INC(i) ;
866 INC(j)
867 END
868 END ;
869 IF j<HighDest
870 THEN
871 dest[j] := nul
872 END ;
873 END StringFormat1 ;
874
875
876 (*
877 FormatError - provides a orthoganal counterpart to the procedure below.
878 *)
879
880 PROCEDURE FormatError (a: ARRAY OF CHAR) ;
881 BEGIN
882 WriteString (StdErr, a)
883 END FormatError ;
884
885
886 (*
887 FormatError1 - generic error procedure taking standard format string
888 and single parameter.
889 *)
890
891 PROCEDURE FormatError1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
892 VAR
893 s: ARRAY [0..MaxErrorString] OF CHAR ;
894 BEGIN
895 StringFormat1 (s, a, w) ;
896 FormatError (s)
897 END FormatError1 ;
898
899
900 (*
901 FormatError2 - generic error procedure taking standard format string
902 and two parameters.
903 *)
904
905 PROCEDURE FormatError2 (a: ARRAY OF CHAR;
906 w1, w2: ARRAY OF BYTE) ;
907 VAR
908 s: ARRAY [0..MaxErrorString] OF CHAR ;
909 BEGIN
910 StringFormat1 (s, a, w1) ;
911 FormatError1 (s, w2)
912 END FormatError2 ;
913
914
915 (*
916 CheckAccess - checks to see whether a file f has been
917 opened for read/write.
918 *)
919
920 PROCEDURE CheckAccess (f: File; use: FileUsage; towrite: BOOLEAN) ;
921 VAR
922 fd: FileDescriptor ;
923 BEGIN
924 IF f#Error
925 THEN
926 fd := GetIndice (FileInfo, f) ;
927 IF fd=NIL
928 THEN
929 IF f#StdErr
930 THEN
931 FormatError ('this file has probably been closed and not reopened successfully or alternatively never opened\n')
932 END ;
933 HALT
934 ELSE
935 WITH fd^ DO
936 IF (use=openedforwrite) AND (usage=openedforread)
937 THEN
938 FormatError1 ('this file (%s) has been opened for reading but is now being written\n',
939 name.address) ;
940 HALT
941 ELSIF (use=openedforread) AND (usage=openedforwrite)
942 THEN
943 FormatError1('this file (%s) has been opened for writing but is now being read\n',
944 name.address) ;
945 HALT
946 ELSIF state=connectionfailure
947 THEN
948 FormatError1('this file (%s) was not successfully opened\n',
949 name.address) ;
950 HALT
951 ELSIF towrite#output
952 THEN
953 IF output
954 THEN
955 FormatError1('this file (%s) was opened for writing but is now being read\n',
956 name.address) ;
957 HALT
958 ELSE
959 FormatError1('this file (%s) was opened for reading but is now being written\n',
960 name.address) ;
961 HALT
962 END
963 END
964 END
965 END
966 ELSE
967 FormatError('this file has not been opened successfully\n') ;
968 HALT
969 END
970 END CheckAccess ;
971
972
973 (*
974 ReadChar - returns a character read from file f.
975 Sensible to check with IsNoError or EOF after calling
976 this function.
977 *)
978
979 PROCEDURE ReadChar (f: File) : CHAR ;
980 VAR
981 ch: CHAR ;
982 BEGIN
983 CheckAccess (f, openedforread, FALSE) ;
984 IF BufferedRead (f, SIZE (ch), ADR (ch)) = VAL (INTEGER, SIZE (ch))
985 THEN
986 SetEndOfLine (f, ch) ;
987 RETURN ch
988 ELSE
989 RETURN nul
990 END
991 END ReadChar ;
992
993
994 (*
995 SetEndOfLine -
996 *)
997
998 PROCEDURE SetEndOfLine (f: File; ch: CHAR) ;
999 VAR
1000 fd: FileDescriptor ;
1001 BEGIN
1002 CheckAccess(f, openedforread, FALSE) ;
1003 IF f#Error
1004 THEN
1005 fd := GetIndice(FileInfo, f) ;
1006 WITH fd^ DO
1007 IF ch=nl
1008 THEN
1009 state := endofline
1010 ELSE
1011 state := successful
1012 END
1013 END
1014 END
1015 END SetEndOfLine ;
1016
1017
1018 (*
1019 UnReadChar - replaces a character, ch, back into file f.
1020 This character must have been read by ReadChar
1021 and it does not allow successive calls. It may
1022 only be called if the previous read was successful
1023 or end of file was seen.
1024 If the state was previously endoffile then it
1025 is altered to successful.
1026 Otherwise it is left alone.
1027 *)
1028
1029 PROCEDURE UnReadChar (f: File; ch: CHAR) ;
1030 VAR
1031 fd : FileDescriptor ;
1032 n : CARDINAL ;
1033 a, b: ADDRESS ;
1034 BEGIN
1035 CheckAccess(f, openedforread, FALSE) ;
1036 IF f#Error
1037 THEN
1038 fd := GetIndice(FileInfo, f) ;
1039 WITH fd^ DO
1040 IF (state=successful) OR (state=endoffile) OR (state=endofline)
1041 THEN
1042 IF (buffer#NIL) AND (buffer^.valid)
1043 THEN
1044 WITH buffer^ DO
1045 (* we assume that a ReadChar has occurred, we will check just in case. *)
1046 IF state=endoffile
1047 THEN
1048 position := MaxBufferLength ;
1049 left := 0 ;
1050 filled := 0 ;
1051 state := successful
1052 END ;
1053 IF position>0
1054 THEN
1055 DEC(position) ;
1056 INC(left) ;
1057 contents^[position] := ch ;
1058 ELSE
1059 (* position=0 *)
1060 (* if possible make room and store ch *)
1061 IF filled=size
1062 THEN
1063 FormatError1('performing too many UnReadChar calls on file (%d)\n', f)
1064 ELSE
1065 n := filled-position ;
1066 b := ADR(contents^[position]) ;
1067 a := ADR(contents^[position+1]) ;
1068 a := memcpy(a, b, n) ;
1069 INC(filled) ;
1070 contents^[position] := ch ;
1071 END
1072 END
1073 END
1074 END
1075 ELSE
1076 FormatError1('UnReadChar can only be called if the previous read was successful or end of file, error on file (%d)\n', f)
1077 END
1078 END
1079 END
1080 END UnReadChar ;
1081
1082
1083 (*
1084 ReadAny - reads HIGH (a) + 1 bytes into, a. All input
1085 is fully buffered, unlike ReadNBytes and thus is more
1086 suited to small reads.
1087 *)
1088
1089 PROCEDURE ReadAny (f: File; VAR a: ARRAY OF BYTE) ;
1090 BEGIN
1091 CheckAccess(f, openedforread, FALSE) ;
1092 IF BufferedRead (f, HIGH (a) + 1, ADR (a)) = VAL (INTEGER, HIGH (a) + 1)
1093 THEN
1094 SetEndOfLine (f, a[HIGH(a)])
1095 END
1096 END ReadAny ;
1097
1098
1099 (*
1100 EOF - tests to see whether a file, f, has reached end of file.
1101 *)
1102
1103 PROCEDURE EOF (f: File) : BOOLEAN ;
1104 VAR
1105 fd: FileDescriptor ;
1106 BEGIN
1107 CheckAccess(f, openedforread, FALSE) ;
1108 IF f#Error
1109 THEN
1110 fd := GetIndice(FileInfo, f) ;
1111 IF fd#NIL
1112 THEN
1113 RETURN( fd^.state=endoffile )
1114 END
1115 END ;
1116 RETURN( TRUE )
1117 END EOF ;
1118
1119
1120 (*
1121 EOLN - tests to see whether a file, f, is upon a newline.
1122 It does NOT consume the newline.
1123 *)
1124
1125 PROCEDURE EOLN (f: File) : BOOLEAN ;
1126 VAR
1127 ch: CHAR ;
1128 fd: FileDescriptor ;
1129 BEGIN
1130 CheckAccess(f, openedforread, FALSE) ;
1131 (*
1132 we will read a character and then push it back onto the input stream,
1133 having noted the file status, we also reset the status.
1134 *)
1135 IF f#Error
1136 THEN
1137 fd := GetIndice(FileInfo, f) ;
1138 IF fd#NIL
1139 THEN
1140 IF (fd^.state=successful) OR (fd^.state=endofline)
1141 THEN
1142 ch := ReadChar(f) ;
1143 IF (fd^.state=successful) OR (fd^.state=endofline)
1144 THEN
1145 UnReadChar(f, ch)
1146 END ;
1147 RETURN( ch=nl )
1148 END
1149 END
1150 END ;
1151 RETURN( FALSE )
1152 END EOLN ;
1153
1154
1155 (*
1156 WasEOLN - tests to see whether a file, f, has just seen a newline.
1157 *)
1158
1159 PROCEDURE WasEOLN (f: File) : BOOLEAN ;
1160 VAR
1161 fd: FileDescriptor ;
1162 BEGIN
1163 CheckAccess(f, openedforread, FALSE) ;
1164 IF f=Error
1165 THEN
1166 RETURN FALSE
1167 ELSE
1168 fd := GetIndice(FileInfo, f) ;
1169 RETURN( (fd#NIL) AND (fd^.state=endofline) )
1170 END
1171 END WasEOLN ;
1172
1173
1174 (*
1175 WriteLine - writes out a linefeed to file, f.
1176 *)
1177
1178 PROCEDURE WriteLine (f: File) ;
1179 BEGIN
1180 WriteChar(f, nl)
1181 END WriteLine ;
1182
1183
1184 (*
1185 WriteNBytes - writes nBytes from memory area src to a file
1186 returning the number of bytes actually written.
1187 This function will flush the buffer and then
1188 write the nBytes using a direct write from libc.
1189 It is ideal for large writes.
1190 *)
1191
1192 PROCEDURE WriteNBytes (f: File; nBytes: CARDINAL; src: ADDRESS) : CARDINAL ;
1193 VAR
1194 total: INTEGER ;
1195 fd : FileDescriptor ;
1196 BEGIN
1197 CheckAccess(f, openedforwrite, TRUE) ;
1198 FlushBuffer(f) ;
1199 IF f#Error
1200 THEN
1201 fd := GetIndice(FileInfo, f) ;
1202 IF fd#NIL
1203 THEN
1204 WITH fd^ DO
1205 total := write(unixfd, src, INTEGER(nBytes)) ;
1206 IF total<0
1207 THEN
1208 state := failed ;
1209 RETURN( 0 )
1210 ELSE
1211 INC(abspos, CARDINAL(total)) ;
1212 IF buffer#NIL
1213 THEN
1214 buffer^.bufstart := abspos
1215 END ;
1216 RETURN( CARDINAL(total) )
1217 END
1218 END
1219 END
1220 END ;
1221 RETURN( 0 )
1222 END WriteNBytes ;
1223
1224
1225 (*
1226 BufferedWrite - will write, nBytes, through the buffer.
1227 Similar to WriteNBytes, but this function will always
1228 write into the buffer before copying into memory.
1229
1230 Useful when performing small writes.
1231 *)
1232
1233 PROCEDURE BufferedWrite (f: File; nBytes: CARDINAL; src: ADDRESS) : INTEGER ;
1234 VAR
1235 dest : ADDRESS ;
1236 total,
1237 n : INTEGER ;
1238 p : POINTER TO BYTE ;
1239 fd : FileDescriptor ;
1240 BEGIN
1241 IF f # Error
1242 THEN
1243 fd := GetIndice (FileInfo, f) ;
1244 IF fd#NIL
1245 THEN
1246 total := 0 ; (* how many bytes have we read *)
1247 WITH fd^ DO
1248 IF buffer # NIL
1249 THEN
1250 WITH buffer^ DO
1251 WHILE nBytes > 0 DO
1252 (* place into the buffer first *)
1253 IF left > 0
1254 THEN
1255 IF nBytes = 1
1256 THEN
1257 (* too expensive to call memcpy for 1 character *)
1258 p := src ;
1259 contents^[position] := p^ ;
1260 DEC (left) ; (* reduce space *)
1261 INC (position) ; (* move onwards n byte *)
1262 INC (total) ;
1263 RETURN( total )
1264 ELSE
1265 n := Min (left, nBytes) ;
1266 dest := address ;
1267 INC (dest, position) ;
1268 p := memcpy (dest, src, CARDINAL (n)) ;
1269 DEC (left, n) ; (* remove consumed bytes *)
1270 INC (position, n) ; (* move onwards n bytes *)
1271 (* move ready for further writes *)
1272 INC (src, n) ;
1273 DEC (nBytes, n) ; (* reduce the amount for future writes *)
1274 INC (total, n)
1275 END
1276 ELSE
1277 FlushBuffer (f) ;
1278 IF (state#successful) AND (state#endofline)
1279 THEN
1280 nBytes := 0
1281 END
1282 END
1283 END
1284 END ;
1285 RETURN( total )
1286 END
1287 END
1288 END
1289 END ;
1290 RETURN( -1 )
1291 END BufferedWrite ;
1292
1293
1294 (*
1295 FlushBuffer - flush contents of file, f.
1296 *)
1297
1298 PROCEDURE FlushBuffer (f: File) ;
1299 VAR
1300 fd: FileDescriptor ;
1301 BEGIN
1302 IF f#Error
1303 THEN
1304 fd := GetIndice(FileInfo, f) ;
1305 IF fd#NIL
1306 THEN
1307 WITH fd^ DO
1308 IF output AND (buffer#NIL)
1309 THEN
1310 WITH buffer^ DO
1311 IF (position=0) OR (write(unixfd, address, position)=VAL(INTEGER, position))
1312 THEN
1313 INC(abspos, position) ;
1314 bufstart := abspos ;
1315 position := 0 ;
1316 filled := 0 ;
1317 left := size
1318 ELSE
1319 state := failed
1320 END
1321 END
1322 END
1323 END
1324 END
1325 END
1326 END FlushBuffer ;
1327
1328
1329 (*
1330 WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output
1331 is fully buffered, unlike WriteNBytes and thus is more
1332 suited to small writes.
1333 *)
1334
1335 PROCEDURE WriteAny (f: File; VAR a: ARRAY OF BYTE) ;
1336 BEGIN
1337 CheckAccess (f, openedforwrite, TRUE) ;
1338 IF BufferedWrite (f, HIGH (a) + 1, ADR (a)) = VAL (INTEGER, HIGH (a) + 1)
1339 THEN
1340 END
1341 END WriteAny ;
1342
1343
1344 (*
1345 WriteChar - writes a single character to file, f.
1346 *)
1347
1348 PROCEDURE WriteChar (f: File; ch: CHAR) ;
1349 BEGIN
1350 CheckAccess (f, openedforwrite, TRUE) ;
1351 IF BufferedWrite (f, SIZE (ch), ADR (ch)) = VAL (INTEGER, SIZE (ch))
1352 THEN
1353 END
1354 END WriteChar ;
1355
1356
1357 (*
1358 WriteCardinal - writes a CARDINAL to file, f.
1359 It writes the binary image of the cardinal
1360 to file, f.
1361 *)
1362
1363 PROCEDURE WriteCardinal (f: File; c: CARDINAL) ;
1364 BEGIN
1365 WriteAny(f, c)
1366 END WriteCardinal ;
1367
1368
1369 (*
1370 ReadCardinal - reads a CARDINAL from file, f.
1371 It reads a binary image of a CARDINAL
1372 from a file, f.
1373 *)
1374
1375 PROCEDURE ReadCardinal (f: File) : CARDINAL ;
1376 VAR
1377 c: CARDINAL ;
1378 BEGIN
1379 ReadAny(f, c) ;
1380 RETURN( c )
1381 END ReadCardinal ;
1382
1383
1384 (*
1385 ReadString - reads a string from file, f, into string, a.
1386 It terminates the string if HIGH is reached or
1387 if a newline is seen or an error occurs.
1388 *)
1389
1390 PROCEDURE ReadString (f: File; VAR a: ARRAY OF CHAR) ;
1391 VAR
1392 high,
1393 i : CARDINAL ;
1394 ch : CHAR ;
1395 BEGIN
1396 CheckAccess(f, openedforread, FALSE) ;
1397 high := HIGH(a) ;
1398 i := 0 ;
1399 REPEAT
1400 ch := ReadChar(f) ;
1401 IF i<=high
1402 THEN
1403 IF (ch=nl) OR (NOT IsNoError(f)) OR EOF(f)
1404 THEN
1405 a[i] := nul ;
1406 INC(i)
1407 ELSE
1408 a[i] := ch ;
1409 INC(i)
1410 END
1411 END
1412 UNTIL (ch=nl) OR (i>high) OR (NOT IsNoError(f)) OR EOF(f)
1413 END ReadString ;
1414
1415
1416 (*
1417 SetPositionFromBeginning - sets the position from the beginning of the file.
1418 *)
1419
1420 PROCEDURE SetPositionFromBeginning (f: File; pos: LONGINT) ;
1421 VAR
1422 offset: LONGINT ;
1423 fd : FileDescriptor ;
1424 BEGIN
1425 IF f#Error
1426 THEN
1427 fd := GetIndice(FileInfo, f) ;
1428 IF fd#NIL
1429 THEN
1430 WITH fd^ DO
1431 (* always force the lseek, until we are confident that abspos is always correct,
1432 basically it needs some hard testing before we should remove the OR TRUE. *)
1433 IF (abspos#pos) OR TRUE
1434 THEN
1435 FlushBuffer(f) ;
1436 IF buffer#NIL
1437 THEN
1438 WITH buffer^ DO
1439 IF output
1440 THEN
1441 left := size
1442 ELSE
1443 left := 0
1444 END ;
1445 position := 0 ;
1446 filled := 0
1447 END
1448 END ;
1449 offset := lseek (unixfd, VAL (COFF_T, pos), SeekSet ()) ;
1450 IF (offset>=0) AND (pos=offset)
1451 THEN
1452 abspos := pos
1453 ELSE
1454 state := failed ;
1455 abspos := 0
1456 END ;
1457 IF buffer#NIL
1458 THEN
1459 buffer^.valid := FALSE ;
1460 buffer^.bufstart := abspos
1461 END
1462 END
1463 END
1464 END
1465 END
1466 END SetPositionFromBeginning ;
1467
1468
1469 (*
1470 SetPositionFromEnd - sets the position from the end of the file.
1471 *)
1472
1473 PROCEDURE SetPositionFromEnd (f: File; pos: LONGINT) ;
1474 VAR
1475 offset: LONGINT ;
1476 fd : FileDescriptor ;
1477 BEGIN
1478 IF f#Error
1479 THEN
1480 fd := GetIndice(FileInfo, f) ;
1481 IF fd#NIL
1482 THEN
1483 WITH fd^ DO
1484 FlushBuffer(f) ;
1485 IF buffer#NIL
1486 THEN
1487 WITH buffer^ DO
1488 IF output
1489 THEN
1490 left := size
1491 ELSE
1492 left := 0
1493 END ;
1494 position := 0 ;
1495 filled := 0
1496 END
1497 END ;
1498 offset := lseek (unixfd, VAL (COFF_T, pos), SeekEnd ()) ;
1499 IF offset>=0
1500 THEN
1501 abspos := offset ;
1502 ELSE
1503 state := failed ;
1504 abspos := 0 ;
1505 offset := 0
1506 END ;
1507 IF buffer#NIL
1508 THEN
1509 buffer^.valid := FALSE ;
1510 buffer^.bufstart := offset
1511 END
1512 END
1513 END
1514 END
1515 END SetPositionFromEnd ;
1516
1517
1518 (*
1519 FindPosition - returns the current absolute position in file, f.
1520 *)
1521
1522 PROCEDURE FindPosition (f: File) : LONGINT ;
1523 VAR
1524 fd: FileDescriptor ;
1525 BEGIN
1526 IF f#Error
1527 THEN
1528 fd := GetIndice(FileInfo, f) ;
1529 IF fd#NIL
1530 THEN
1531 WITH fd^ DO
1532 IF (buffer=NIL) OR (NOT buffer^.valid)
1533 THEN
1534 RETURN( abspos )
1535 ELSE
1536 WITH buffer^ DO
1537 RETURN( bufstart+VAL(LONGINT, position) )
1538 END
1539 END
1540 END
1541 END
1542 END ;
1543 RETURN( 0 )
1544 END FindPosition ;
1545
1546
1547 (*
1548 GetFileName - assigns, a, with the filename associated with, f.
1549 *)
1550
1551 PROCEDURE GetFileName (f: File; VAR a: ARRAY OF CHAR) ;
1552 VAR
1553 i : CARDINAL ;
1554 p : POINTER TO CHAR ;
1555 fd: FileDescriptor ;
1556 BEGIN
1557 IF f#Error
1558 THEN
1559 fd := GetIndice(FileInfo, f) ;
1560 IF fd=NIL
1561 THEN
1562 FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ;
1563 HALT
1564 ELSE
1565 WITH fd^.name DO
1566 IF address=NIL
1567 THEN
1568 StrCopy('', a)
1569 ELSE
1570 p := address ;
1571 i := 0 ;
1572 WHILE (p^#nul) AND (i<=HIGH(a)) DO
1573 a[i] := p^ ;
1574 INC(p) ;
1575 INC(i)
1576 END
1577 END
1578 END
1579 END
1580 END
1581 END GetFileName ;
1582
1583
1584 (*
1585 getFileName - returns the address of the filename associated with, f.
1586 *)
1587
1588 PROCEDURE getFileName (f: File) : ADDRESS ;
1589 VAR
1590 fd: FileDescriptor ;
1591 BEGIN
1592 IF f#Error
1593 THEN
1594 fd := GetIndice(FileInfo, f) ;
1595 IF fd=NIL
1596 THEN
1597 FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ;
1598 HALT
1599 ELSE
1600 RETURN fd^.name.address
1601 END
1602 END ;
1603 RETURN NIL
1604 END getFileName ;
1605
1606
1607 (*
1608 getFileNameLength - returns the number of characters associated with filename, f.
1609 *)
1610
1611 PROCEDURE getFileNameLength (f: File) : CARDINAL ;
1612 VAR
1613 fd: FileDescriptor ;
1614 BEGIN
1615 IF f#Error
1616 THEN
1617 fd := GetIndice(FileInfo, f) ;
1618 IF fd=NIL
1619 THEN
1620 FormatError('this file has probably been closed and not reopened successfully or alternatively never opened\n') ;
1621 HALT
1622 ELSE
1623 RETURN fd^.name.size
1624 END
1625 END ;
1626 RETURN 0
1627 END getFileNameLength ;
1628
1629
1630 (*
1631 PreInitialize - preinitialize the file descriptor.
1632 *)
1633
1634 PROCEDURE PreInitialize (f: File; fname: ARRAY OF CHAR;
1635 state: FileStatus; use: FileUsage;
1636 towrite: BOOLEAN; osfd: INTEGER; bufsize: CARDINAL) ;
1637 VAR
1638 fd, fe: FileDescriptor ;
1639 BEGIN
1640 IF InitializeFile(f, ADR(fname), StrLen(fname), state, use, towrite, bufsize)=f
1641 THEN
1642 fd := GetIndice(FileInfo, f) ;
1643 IF f=Error
1644 THEN
1645 fe := GetIndice(FileInfo, StdErr) ;
1646 IF fe=NIL
1647 THEN
1648 HALT
1649 ELSE
1650 fd^.unixfd := fe^.unixfd (* the error channel *)
1651 END
1652 ELSE
1653 fd^.unixfd := osfd
1654 END
1655 ELSE
1656 HALT
1657 END
1658 END PreInitialize ;
1659
1660
1661 (*
1662 FlushOutErr - flushes, StdOut, and, StdErr.
1663 It is also called when the application calls M2RTS.Terminate.
1664 (which is automatically placed in program modules by the GM2
1665 scaffold).
1666 *)
1667
1668 PROCEDURE FlushOutErr ;
1669 BEGIN
1670 IF IsNoError(StdOut)
1671 THEN
1672 FlushBuffer(StdOut)
1673 END ;
1674 IF IsNoError(StdErr)
1675 THEN
1676 FlushBuffer(StdErr)
1677 END
1678 END FlushOutErr ;
1679
1680
1681 (*
1682 Init - initialize the modules, global variables.
1683 *)
1684
1685 PROCEDURE Init ;
1686 BEGIN
1687 FileInfo := InitIndex(0) ;
1688 Error := 0 ;
1689 PreInitialize(Error , 'error' , toomanyfilesopen, unused , FALSE, -1, 0) ;
1690 StdIn := 1 ;
1691 PreInitialize(StdIn , '<stdin>' , successful , openedforread , FALSE, 0, MaxBufferLength) ;
1692 StdOut := 2 ;
1693 PreInitialize(StdOut , '<stdout>', successful , openedforwrite, TRUE, 1, MaxBufferLength) ;
1694 StdErr := 3 ;
1695 PreInitialize(StdErr , '<stderr>', successful , openedforwrite, TRUE, 2, MaxBufferLength) ;
1696 IF NOT InstallTerminationProcedure(FlushOutErr)
1697 THEN
1698 HALT
1699 END
1700 END Init ;
1701
1702
1703 BEGIN
1704 Init
1705 FINALLY
1706 FlushOutErr
1707 END FIO.