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)
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/>. */
31 # if !defined (PROC_D)
33 typedef void (*PROC_t
) (void);
34 typedef struct { PROC_t proc
; } PROC
;
51 WriteLn - writes a carriage return and a newline
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
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
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)
106 Echo - echos the character, ch, onto the output channel if IsATTY
110 static void Echo (char 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
136 extern "C" void StrIO_WriteLn (void)
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
149 extern "C" void StrIO_ReadString (char *a
, unsigned int _a_high
)
159 if ((ch
== ASCII_del
) || (ch
== ASCII_bs
))
163 StdIO_Write (ASCII_bel
);
171 else if (ch
== ASCII_nak
)
173 /* avoid dangling else. */
180 else if (ch
== ASCII_etb
)
182 /* avoid dangling else. */
187 else if (AlphaNum (a
[n
-1]))
189 /* avoid dangling else. */
193 } while (! ((n
== 0) || (! (AlphaNum (a
[n
-1])))));
197 /* avoid dangling else. */
204 /* avoid dangling else. */
205 if ((ch
== ASCII_cr
) || (ch
== ASCII_lf
))
207 const_cast<char *>(a
)[n
] = ASCII_nul
;
210 else if (ch
== ASCII_ff
)
212 /* avoid dangling else. */
213 const_cast<char *>(a
)[0] = ch
;
216 const_cast<char *>(a
)[1] = ASCII_nul
;
222 /* avoid dangling else. */
224 const_cast<char *>(a
)[n
] = ch
;
227 else if (ch
== ASCII_eof
)
229 /* avoid dangling else. */
230 const_cast<char *>(a
)[n
] = ch
;
235 const_cast<char *>(a
)[n
] = ASCII_nul
;
239 else if (ch
!= ASCII_cr
)
241 /* avoid dangling else. */
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
)
258 /* make a local copy of each unbounded array. */
259 memcpy (a
, a_
, _a_high
+1);
263 while ((n
<= high
) && (a
[n
] != ASCII_nul
))
270 extern "C" void _M2_StrIO_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
272 /* IsATTY := isatty() */
276 extern "C" void _M2_StrIO_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])