c++: add fixed test [PR94100]
[official-gcc.git] / gcc / m2 / mc-boot / GStrIO.cc
blob533460b63f706d01d60955d6c95eadc42e2b07fc
1 /* do not edit automatically generated by mc from StrIO. */
2 /* StrIO.mod provides simple string input output routines.
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
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.
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.
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.
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/>. */
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
37 # if !defined (FALSE)
38 # define FALSE (1==0)
39 # endif
41 #define _StrIO_C
43 #include "GStrIO.h"
44 # include "GASCII.h"
45 # include "GStdIO.h"
46 # include "Glibc.h"
48 static bool IsATTY;
51 WriteLn - writes a carriage return and a newline
52 character.
55 extern "C" void StrIO_WriteLn (void);
58 ReadString - reads a sequence of characters into a string.
59 Line editing accepts Del, Ctrl H, Ctrl W and
60 Ctrl U.
63 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high);
66 WriteString - writes a string to the default output.
69 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high);
72 Erase - writes a backspace, space and backspace to remove the
73 last character displayed.
76 static void Erase (void);
79 Echo - echos the character, ch, onto the output channel if IsATTY
80 is true.
83 static void Echo (char ch);
86 AlphaNum- returns true if character, ch, is an alphanumeric character.
89 static bool AlphaNum (char ch);
93 Erase - writes a backspace, space and backspace to remove the
94 last character displayed.
97 static void Erase (void)
99 Echo (ASCII_bs);
100 Echo (' ');
101 Echo (ASCII_bs);
106 Echo - echos the character, ch, onto the output channel if IsATTY
107 is true.
110 static void Echo (char ch)
112 if (IsATTY)
114 StdIO_Write (ch);
120 AlphaNum- returns true if character, ch, is an alphanumeric character.
123 static bool AlphaNum (char ch)
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 ();
132 WriteLn - writes a carriage return and a newline
133 character.
136 extern "C" void StrIO_WriteLn (void)
138 Echo (ASCII_cr);
139 StdIO_Write (ASCII_lf);
144 ReadString - reads a sequence of characters into a string.
145 Line editing accepts Del, Ctrl H, Ctrl W and
146 Ctrl U.
149 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high)
151 unsigned int n;
152 unsigned int high;
153 char ch;
155 high = _a_high;
156 n = 0;
157 do {
158 StdIO_Read (&ch);
159 if ((ch == ASCII_del) || (ch == ASCII_bs))
161 if (n == 0)
163 StdIO_Write (ASCII_bel);
165 else
167 Erase ();
168 n -= 1;
171 else if (ch == ASCII_nak)
173 /* avoid dangling else. */
174 while (n > 0)
176 Erase ();
177 n -= 1;
180 else if (ch == ASCII_etb)
182 /* avoid dangling else. */
183 if (n == 0)
185 Echo (ASCII_bel);
187 else if (AlphaNum (a[n-1]))
189 /* avoid dangling else. */
190 do {
191 Erase ();
192 n -= 1;
193 } while (! ((n == 0) || (! (AlphaNum (a[n-1])))));
195 else
197 /* avoid dangling else. */
198 Erase ();
199 n -= 1;
202 else if (n <= high)
204 /* avoid dangling else. */
205 if ((ch == ASCII_cr) || (ch == ASCII_lf))
207 const_cast<char *>(a)[n] = ASCII_nul;
208 n += 1;
210 else if (ch == ASCII_ff)
212 /* avoid dangling else. */
213 const_cast<char *>(a)[0] = ch;
214 if (high > 0)
216 const_cast<char *>(a)[1] = ASCII_nul;
218 ch = ASCII_cr;
220 else if (ch >= ' ')
222 /* avoid dangling else. */
223 Echo (ch);
224 const_cast<char *>(a)[n] = ch;
225 n += 1;
227 else if (ch == ASCII_eof)
229 /* avoid dangling else. */
230 const_cast<char *>(a)[n] = ch;
231 n += 1;
232 ch = ASCII_cr;
233 if (n <= high)
235 const_cast<char *>(a)[n] = ASCII_nul;
239 else if (ch != ASCII_cr)
241 /* avoid dangling else. */
242 Echo (ASCII_bel);
244 } while (! ((ch == ASCII_cr) || (ch == ASCII_lf)));
249 WriteString - writes a string to the default output.
252 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high)
254 unsigned int n;
255 unsigned int high;
256 char a[_a_high+1];
258 /* make a local copy of each unbounded array. */
259 memcpy (a, a_, _a_high+1);
261 high = _a_high;
262 n = 0;
263 while ((n <= high) && (a[n] != ASCII_nul))
265 StdIO_Write (a[n]);
266 n += 1;
270 extern "C" void _M2_StrIO_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
272 /* IsATTY := isatty() */
273 IsATTY = false;
276 extern "C" void _M2_StrIO_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])