]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/mc-boot/GStrIO.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / mc-boot / GStrIO.c
1 /* do not edit automatically generated by mc from StrIO. */
2 /* StrIO.mod provides simple string input output routines.
3
4 Copyright (C) 2001-2023 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
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 "config.h"
29 #include "system.h"
30 # if !defined (PROC_D)
31 # define PROC_D
32 typedef void (*PROC_t) (void);
33 typedef struct { PROC_t proc; } PROC;
34 # endif
35
36 # if !defined (FALSE)
37 # define FALSE (1==0)
38 # endif
39
40 #define _StrIO_H
41 #define _StrIO_C
42
43 # include "GASCII.h"
44 # include "GStdIO.h"
45 # include "Glibc.h"
46
47 static unsigned int IsATTY;
48
49 /*
50 WriteLn - writes a carriage return and a newline
51 character.
52 */
53
54 extern "C" void StrIO_WriteLn (void);
55
56 /*
57 ReadString - reads a sequence of characters into a string.
58 Line editing accepts Del, Ctrl H, Ctrl W and
59 Ctrl U.
60 */
61
62 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high);
63
64 /*
65 WriteString - writes a string to the default output.
66 */
67
68 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high);
69
70 /*
71 Erase - writes a backspace, space and backspace to remove the
72 last character displayed.
73 */
74
75 static void Erase (void);
76
77 /*
78 Echo - echos the character, ch, onto the output channel if IsATTY
79 is true.
80 */
81
82 static void Echo (char ch);
83
84 /*
85 AlphaNum- returns true if character, ch, is an alphanumeric character.
86 */
87
88 static unsigned int AlphaNum (char ch);
89
90
91 /*
92 Erase - writes a backspace, space and backspace to remove the
93 last character displayed.
94 */
95
96 static void Erase (void)
97 {
98 Echo (ASCII_bs);
99 Echo (' ');
100 Echo (ASCII_bs);
101 }
102
103
104 /*
105 Echo - echos the character, ch, onto the output channel if IsATTY
106 is true.
107 */
108
109 static void Echo (char ch)
110 {
111 if (IsATTY)
112 {
113 StdIO_Write (ch);
114 }
115 }
116
117
118 /*
119 AlphaNum- returns true if character, ch, is an alphanumeric character.
120 */
121
122 static unsigned int AlphaNum (char ch)
123 {
124 return (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9'));
125 /* static analysis guarentees a RETURN statement will be used before here. */
126 __builtin_unreachable ();
127 }
128
129
130 /*
131 WriteLn - writes a carriage return and a newline
132 character.
133 */
134
135 extern "C" void StrIO_WriteLn (void)
136 {
137 Echo (ASCII_cr);
138 StdIO_Write (ASCII_lf);
139 }
140
141
142 /*
143 ReadString - reads a sequence of characters into a string.
144 Line editing accepts Del, Ctrl H, Ctrl W and
145 Ctrl U.
146 */
147
148 extern "C" void StrIO_ReadString (char *a, unsigned int _a_high)
149 {
150 unsigned int n;
151 unsigned int high;
152 char ch;
153
154 high = _a_high;
155 n = 0;
156 do {
157 StdIO_Read (&ch);
158 if ((ch == ASCII_del) || (ch == ASCII_bs))
159 {
160 if (n == 0)
161 {
162 StdIO_Write (ASCII_bel);
163 }
164 else
165 {
166 Erase ();
167 n -= 1;
168 }
169 }
170 else if (ch == ASCII_nak)
171 {
172 /* avoid dangling else. */
173 while (n > 0)
174 {
175 Erase ();
176 n -= 1;
177 }
178 }
179 else if (ch == ASCII_etb)
180 {
181 /* avoid dangling else. */
182 if (n == 0)
183 {
184 Echo (ASCII_bel);
185 }
186 else if (AlphaNum (a[n-1]))
187 {
188 /* avoid dangling else. */
189 do {
190 Erase ();
191 n -= 1;
192 } while (! ((n == 0) || (! (AlphaNum (a[n-1])))));
193 }
194 else
195 {
196 /* avoid dangling else. */
197 Erase ();
198 n -= 1;
199 }
200 }
201 else if (n <= high)
202 {
203 /* avoid dangling else. */
204 if ((ch == ASCII_cr) || (ch == ASCII_lf))
205 {
206 a[n] = ASCII_nul;
207 n += 1;
208 }
209 else if (ch == ASCII_ff)
210 {
211 /* avoid dangling else. */
212 a[0] = ch;
213 if (high > 0)
214 {
215 a[1] = ASCII_nul;
216 }
217 ch = ASCII_cr;
218 }
219 else if (ch >= ' ')
220 {
221 /* avoid dangling else. */
222 Echo (ch);
223 a[n] = ch;
224 n += 1;
225 }
226 else if (ch == ASCII_eof)
227 {
228 /* avoid dangling else. */
229 a[n] = ch;
230 n += 1;
231 ch = ASCII_cr;
232 if (n <= high)
233 {
234 a[n] = ASCII_nul;
235 }
236 }
237 }
238 else if (ch != ASCII_cr)
239 {
240 /* avoid dangling else. */
241 Echo (ASCII_bel);
242 }
243 } while (! ((ch == ASCII_cr) || (ch == ASCII_lf)));
244 }
245
246
247 /*
248 WriteString - writes a string to the default output.
249 */
250
251 extern "C" void StrIO_WriteString (const char *a_, unsigned int _a_high)
252 {
253 unsigned int n;
254 unsigned int high;
255 char a[_a_high+1];
256
257 /* make a local copy of each unbounded array. */
258 memcpy (a, a_, _a_high+1);
259
260 high = _a_high;
261 n = 0;
262 while ((n <= high) && (a[n] != ASCII_nul))
263 {
264 StdIO_Write (a[n]);
265 n += 1;
266 }
267 }
268
269 extern "C" void _M2_StrIO_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
270 {
271 /* IsATTY := isatty() */
272 IsATTY = FALSE;
273 }
274
275 extern "C" void _M2_StrIO_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
276 {
277 }