Debian Codegraph @

gcc/m2/pge-boot/GStrIO.cc

raw
1 /* do not edit automatically generated by mc from StrIO. */
2 /* StrIO.mod provides simple string input output routines.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include <stdbool.h>
29 # if !defined (PROC_D)
30 # define PROC_D
31 typedef void (*PROC_t) (void);
32 typedef struct { PROC_t proc; } PROC;
33 # endif
34
35 # if !defined (FALSE)
36 # define FALSE (1==0)
37 # endif
38
39 #include <string.h>
40 #include <limits.h>
41 #define _StrIO_C
42
43 #include "GStrIO.h"
44 # include "GASCII.h"
45 # include "GStdIO.h"
46 # include "Glibc.h"
47
48 static bool IsATTY;
49
50 /*
51 WriteLn - writes a carriage return and a newline
52 character.
53 */
54
55 extern "C" void StrIO_WriteLn (void);
56
57 /*
58 ReadString - reads a sequence of characters into a string.
59 Line editing accepts Del, Ctrl H, Ctrl W and
60 Ctrl U.
61 */
62
63 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high);
64
65 /*
66 WriteString - writes a string to the default output.
67 */
68
69 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high);
70
71 /*
72 Erase - writes a backspace, space and backspace to remove the
73 last character displayed.
74 */
75
76 static void Erase (void);
77
78 /*
79 Echo - echos the character, ch, onto the output channel if IsATTY
80 is true.
81 */
82
83 static void Echo (char ch);
84
85 /*
86 AlphaNum- returns true if character, ch, is an alphanumeric character.
87 */
88
89 static bool AlphaNum (char ch);
90
91
92 /*
93 Erase - writes a backspace, space and backspace to remove the
94 last character displayed.
95 */
96
97 static void Erase (void)
98 {
99 Echo (ASCII_bs);
100 Echo (' ');
101 Echo (ASCII_bs);
102 }
103
104
105 /*
106 Echo - echos the character, ch, onto the output channel if IsATTY
107 is true.
108 */
109
110 static void Echo (char ch)
111 {
112 if (IsATTY)
113 {
114 StdIO_Write (ch);
115 }
116 }
117
118
119 /*
120 AlphaNum- returns true if character, ch, is an alphanumeric character.
121 */
122
123 static bool AlphaNum (char ch)
124 {
125 return (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9'));
126 /* static analysis guarentees a RETURN statement will be used before here. */
127 __builtin_unreachable ();
128 }
129
130
131 /*
132 WriteLn - writes a carriage return and a newline
133 character.
134 */
135
136 extern "C" void StrIO_WriteLn (void)
137 {
138 Echo (ASCII_cr);
139 StdIO_Write (ASCII_lf);
140 }
141
142
143 /*
144 ReadString - reads a sequence of characters into a string.
145 Line editing accepts Del, Ctrl H, Ctrl W and
146 Ctrl U.
147 */
148
149 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high)
150 {
151 unsigned int n;
152 unsigned int high;
153 char ch;
154
155 high = _a_high;
156 n = 0;
157 do {
158 StdIO_Read (&ch);
159 if ((ch == ASCII_del) || (ch == ASCII_bs))
160 {
161 if (n == 0)
162 {
163 StdIO_Write (ASCII_bel);
164 }
165 else
166 {
167 Erase ();
168 n -= 1;
169 }
170 }
171 else if (ch == ASCII_nak)
172 {
173 /* avoid dangling else. */
174 while (n > 0)
175 {
176 Erase ();
177 n -= 1;
178 }
179 }
180 else if (ch == ASCII_etb)
181 {
182 /* avoid dangling else. */
183 if (n == 0)
184 {
185 Echo (ASCII_bel);
186 }
187 else if (AlphaNum (a[n-1]))
188 {
189 /* avoid dangling else. */
190 do {
191 Erase ();
192 n -= 1;
193 } while (! ((n == 0) || (! (AlphaNum (a[n-1])))));
194 }
195 else
196 {
197 /* avoid dangling else. */
198 Erase ();
199 n -= 1;
200 }
201 }
202 else if (n <= high)
203 {
204 /* avoid dangling else. */
205 if ((ch == ASCII_cr) || (ch == ASCII_lf))
206 {
207 const_cast<char *>(a)[n] = ASCII_nul;
208 n += 1;
209 }
210 else if (ch == ASCII_ff)
211 {
212 /* avoid dangling else. */
213 const_cast<char *>(a)[0] = ch;
214 if (high > 0)
215 {
216 const_cast<char *>(a)[1] = ASCII_nul;
217 }
218 ch = ASCII_cr;
219 }
220 else if (ch >= ' ')
221 {
222 /* avoid dangling else. */
223 Echo (ch);
224 const_cast<char *>(a)[n] = ch;
225 n += 1;
226 }
227 else if (ch == ASCII_eof)
228 {
229 /* avoid dangling else. */
230 const_cast<char *>(a)[n] = ch;
231 n += 1;
232 ch = ASCII_cr;
233 if (n <= high)
234 {
235 const_cast<char *>(a)[n] = ASCII_nul;
236 }
237 }
238 }
239 else if (ch != ASCII_cr)
240 {
241 /* avoid dangling else. */
242 Echo (ASCII_bel);
243 }
244 } while (! ((ch == ASCII_cr) || (ch == ASCII_lf)));
245 }
246
247
248 /*
249 WriteString - writes a string to the default output.
250 */
251
252 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high)
253 {
254 unsigned int n;
255 unsigned int high;
256 char a[_a_high+1];
257
258 /* make a local copy of each unbounded array. */
259 memcpy (a, a_, _a_high+1);
260
261 high = _a_high;
262 n = 0;
263 while ((n <= high) && (a[n] != ASCII_nul))
264 {
265 StdIO_Write (a[n]);
266 n += 1;
267 }
268 }
269
270 extern "C" void _M2_StrIO_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
271 {
272 /* IsATTY := isatty() */
273 IsATTY = false;
274 }
275
276 extern "C" void _M2_StrIO_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
277 {
278 }