]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs/StrIO.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / StrIO.mod
CommitLineData
1eee94d3
GM
1(* StrIO.mod provides simple string input output routines.
2
83ffe9cd 3Copyright (C) 2001-2023 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. *)
26
27IMPLEMENTATION MODULE StrIO ;
28
29
30FROM ASCII IMPORT cr, nul, lf, bel, del, bs, nak, etb, ff, eof ;
31FROM StdIO IMPORT Read, Write ;
32FROM libc IMPORT isatty ;
33
34
35VAR
36 IsATTY: BOOLEAN ; (* Is default input from the keyboard? *)
37
38
39(*
40 WriteLn - writes a carriage return and a newline
41 character.
42*)
43
44PROCEDURE WriteLn ;
45BEGIN
46 Echo(cr) ;
47 Write(lf)
48END WriteLn ;
49
50
51(*
52 ReadString - reads a sequence of characters into a string.
53 Line editing accepts Del, Ctrl H, Ctrl W and
54 Ctrl U.
55*)
56
57PROCEDURE ReadString (VAR a: ARRAY OF CHAR) ;
58VAR
59 n ,
60 high : CARDINAL ;
61 ch : CHAR ;
62BEGIN
63 high := HIGH(a) ;
64 n := 0 ;
65 REPEAT
66 Read(ch) ;
67 IF (ch=del) OR (ch=bs)
68 THEN
69 IF n=0
70 THEN
71 Write(bel)
72 ELSE
73 Erase ;
74 DEC(n)
75 END
76 ELSIF ch=nak (* Ctrl U *)
77 THEN
78 WHILE n>0 DO
79 Erase ;
80 DEC(n)
81 END
82 ELSIF ch=etb (* Ctrl W *)
83 THEN
84 IF n=0
85 THEN
86 Echo(bel)
87 ELSIF AlphaNum(a[n-1])
88 THEN
89 REPEAT
90 Erase ;
91 DEC(n)
92 UNTIL (n=0) OR (NOT AlphaNum(a[n-1]))
93 ELSE
94 Erase ;
95 DEC(n)
96 END
97 ELSIF n<=high
98 THEN
99 IF (ch=cr) OR (ch=lf)
100 THEN
101 a[n] := nul ;
102 INC(n)
103 ELSIF ch=ff
104 THEN
105 a[0] := ch ;
106 IF high>0
107 THEN
108 a[1] := nul
109 END ;
110 ch := cr
111 ELSIF ch>=' '
112 THEN
113 Echo(ch) ;
114 a[n] := ch ;
115 INC(n)
116 ELSIF ch=eof
117 THEN
118 a[n] := ch ;
119 INC(n) ;
120 ch := cr;
121 IF n<=high
122 THEN
123 a[n] := nul
124 END
125 END
126 ELSIF ch#cr
127 THEN
128 Echo(bel)
129 END
130 UNTIL (ch=cr) OR (ch=lf)
131END ReadString ;
132
133
134(*
135 WriteString - writes a string to the default output.
136*)
137
138PROCEDURE WriteString (a: ARRAY OF CHAR) ;
139VAR
140 n ,
141 high : CARDINAL ;
142BEGIN
143 high := HIGH(a) ;
144 n := 0 ;
145 WHILE (n <= high) AND (a[n] # nul) DO
146 Write(a[n]) ;
147 INC(n)
148 END
149END WriteString ;
150
151
152(*
153 Erase - writes a backspace, space and backspace to remove the
154 last character displayed.
155*)
156
157PROCEDURE Erase ;
158BEGIN
159 Echo(bs) ;
160 Echo(' ') ;
161 Echo(bs)
162END Erase ;
163
164
165(*
166 Echo - echos the character, ch, onto the output channel if IsATTY
167 is true.
168*)
169
170PROCEDURE Echo (ch: CHAR) ;
171BEGIN
172 IF IsATTY
173 THEN
174 Write(ch)
175 END
176END Echo ;
177
178
179(*
180 AlphaNum- returns true if character, ch, is an alphanumeric character.
181*)
182
183PROCEDURE AlphaNum (ch: CHAR) : BOOLEAN ;
184BEGIN
185 RETURN ((ch>='a') AND (ch<='z')) OR
186 ((ch>='A') AND (ch<='Z')) OR
187 ((ch>='0') AND (ch<='9'))
188END AlphaNum ;
189
190
191BEGIN
192(* IsATTY := isatty() *)
193 IsATTY := FALSE
194END StrIO.