Debian Codegraph @

gcc/m2/gm2-libs/StdIO.mod

raw
1 (* StdIO.mod provides general Read and Write procedures.
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 StdIO ;
28
29 IMPORT IO ;
30
31
32 CONST
33 MaxStack = 40 ;
34
35 VAR
36 StackW : ARRAY [0..MaxStack] OF ProcWrite ;
37 StackWPtr: CARDINAL ;
38 StackR : ARRAY [0..MaxStack] OF ProcRead ;
39 StackRPtr: CARDINAL ;
40
41
42 (*
43 Read - is the generic procedure that all higher application layers
44 should use to receive a character.
45 *)
46
47 PROCEDURE Read (VAR ch: CHAR) ;
48 BEGIN
49 StackR[StackRPtr](ch)
50 END Read ;
51
52
53 (*
54 Write - is the generic procedure that all higher application layers
55 should use to emit a character.
56 *)
57
58 PROCEDURE Write (ch: CHAR) ;
59 BEGIN
60 StackW[StackWPtr](ch)
61 END Write ;
62
63
64 (*
65 PushOutput - pushes the current Write procedure onto a stack,
66 any future references to Write will actually invoke
67 procedure, p.
68 *)
69
70 PROCEDURE PushOutput (p: ProcWrite) ;
71 BEGIN
72 IF StackWPtr=MaxStack
73 THEN
74 HALT
75 ELSE
76 INC(StackWPtr) ;
77 StackW[StackWPtr] := p
78 END
79 END PushOutput ;
80
81
82 (*
83 PopOutput - restores Write to use the previous output procedure.
84 *)
85
86 PROCEDURE PopOutput ;
87 BEGIN
88 IF StackWPtr=1
89 THEN
90 HALT
91 ELSE
92 DEC(StackWPtr)
93 END
94 END PopOutput ;
95
96
97 (*
98 GetCurrentOutput - returns the current output procedure.
99 *)
100
101 PROCEDURE GetCurrentOutput () : ProcWrite ;
102 BEGIN
103 IF StackWPtr>0
104 THEN
105 RETURN( StackW[StackWPtr] )
106 ELSE
107 HALT
108 END
109 END GetCurrentOutput ;
110
111
112 (*
113 PushInput - pushes the current Read procedure onto a stack,
114 any future references to Read will actually invoke
115 procedure, p.
116 *)
117
118 PROCEDURE PushInput (p: ProcRead) ;
119 BEGIN
120 IF StackRPtr=MaxStack
121 THEN
122 HALT
123 ELSE
124 INC(StackRPtr) ;
125 StackR[StackRPtr] := p
126 END
127 END PushInput ;
128
129
130 (*
131 PopInput - restores Write to use the previous output procedure.
132 *)
133
134 PROCEDURE PopInput ;
135 BEGIN
136 IF StackRPtr=1
137 THEN
138 HALT
139 ELSE
140 DEC(StackRPtr)
141 END
142 END PopInput ;
143
144
145 (*
146 GetCurrentInput - returns the current input procedure.
147 *)
148
149 PROCEDURE GetCurrentInput () : ProcRead ;
150 BEGIN
151 IF StackRPtr>0
152 THEN
153 RETURN( StackR[StackRPtr] )
154 ELSE
155 HALT
156 END
157 END GetCurrentInput ;
158
159
160 BEGIN
161 StackWPtr := 0 ;
162 StackRPtr := 0 ;
163 PushOutput(IO.Write) ;
164 PushInput(IO.Read)
165 END StdIO.