1 /* do not edit automatically generated by mc from PushBackInput. */
2 /* PushBackInput.mod provides a method for pushing back and consuming input.
4 Copyright (C) 2001-2024 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
;
41 #if defined(__cplusplus)
45 #define _PushBackInput_C
47 #include "GPushBackInput.h"
49 # include "GDynamicStrings.h"
53 # include "GNumberIO.h"
58 # define MaxPushBackStack 8192
59 # define MaxFileName 4096
60 typedef struct PushBackInput__T2_a PushBackInput__T2
;
62 typedef struct PushBackInput__T3_a PushBackInput__T3
;
64 struct PushBackInput__T2_a
{ char array
[MaxFileName
+1]; };
65 struct PushBackInput__T3_a
{ char array
[MaxPushBackStack
+1]; };
66 static PushBackInput__T2 FileName
;
67 static PushBackInput__T3 CharStack
;
68 static unsigned int ExitStatus
;
69 static unsigned int Column
;
70 static unsigned int StackPtr
;
71 static unsigned int LineNo
;
72 static bool Debugging
;
75 Open - opens a file for reading.
78 extern "C" FIO_File
PushBackInput_Open (const char *a_
, unsigned int _a_high
);
81 GetCh - gets a character from either the push back stack or
85 extern "C" char PushBackInput_GetCh (FIO_File f
);
88 PutCh - pushes a character onto the push back stack, it also
89 returns the character which has been pushed.
92 extern "C" char PushBackInput_PutCh (char ch
);
95 PutString - pushes a string onto the push back stack.
98 extern "C" void PushBackInput_PutString (const char *a_
, unsigned int _a_high
);
101 PutStr - pushes a dynamic string onto the push back stack.
102 The string, s, is not deallocated.
105 extern "C" void PushBackInput_PutStr (DynamicStrings_String s
);
108 Error - emits an error message with the appropriate file, line combination.
111 extern "C" void PushBackInput_Error (const char *a_
, unsigned int _a_high
);
114 WarnError - emits an error message with the appropriate file, line combination.
115 It does not terminate but when the program finishes an exit status of
119 extern "C" void PushBackInput_WarnError (const char *a_
, unsigned int _a_high
);
122 WarnString - emits an error message with the appropriate file, line combination.
123 It does not terminate but when the program finishes an exit status of
127 extern "C" void PushBackInput_WarnString (DynamicStrings_String s
);
130 Close - closes the opened file.
133 extern "C" void PushBackInput_Close (FIO_File f
);
136 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
139 extern "C" unsigned int PushBackInput_GetExitStatus (void);
142 SetDebug - sets the debug flag on or off.
145 extern "C" void PushBackInput_SetDebug (bool d
);
148 GetColumnPosition - returns the column position of the current character.
151 extern "C" unsigned int PushBackInput_GetColumnPosition (void);
154 GetCurrentLine - returns the current line number.
157 extern "C" unsigned int PushBackInput_GetCurrentLine (void);
160 ErrChar - writes a char, ch, to stderr.
163 static void ErrChar (char ch
);
166 Init - initialize global variables.
169 static void Init (void);
173 ErrChar - writes a char, ch, to stderr.
176 static void ErrChar (char ch
)
178 FIO_WriteChar (FIO_StdErr
, ch
);
183 Init - initialize global variables.
186 static void Init (void)
196 Open - opens a file for reading.
199 extern "C" FIO_File
PushBackInput_Open (const char *a_
, unsigned int _a_high
)
203 /* make a local copy of each unbounded array. */
204 memcpy (a
, a_
, _a_high
+1);
207 StrLib_StrCopy ((const char *) a
, _a_high
, (char *) &FileName
.array
[0], MaxFileName
);
208 return FIO_OpenToRead ((const char *) a
, _a_high
);
209 /* static analysis guarentees a RETURN statement will be used before here. */
210 __builtin_unreachable ();
215 GetCh - gets a character from either the push back stack or
219 extern "C" char PushBackInput_GetCh (FIO_File f
)
228 StdIO_Write (CharStack
.array
[StackPtr
]);
230 return CharStack
.array
[StackPtr
];
234 if ((FIO_EOF (f
)) || (! (FIO_IsNoError (f
))))
241 ch
= FIO_ReadChar (f
);
242 } while (! (((ch
!= ASCII_cr
) || (FIO_EOF (f
))) || (! (FIO_IsNoError (f
)))));
259 /* static analysis guarentees a RETURN statement will be used before here. */
260 __builtin_unreachable ();
265 PutCh - pushes a character onto the push back stack, it also
266 returns the character which has been pushed.
269 extern "C" char PushBackInput_PutCh (char ch
)
271 if (StackPtr
< MaxPushBackStack
)
273 CharStack
.array
[StackPtr
] = ch
;
278 Debug_Halt ((const char *) "max push back stack exceeded, increase MaxPushBackStack", 55, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutCh", 5, 151);
281 /* static analysis guarentees a RETURN statement will be used before here. */
282 __builtin_unreachable ();
287 PutString - pushes a string onto the push back stack.
290 extern "C" void PushBackInput_PutString (const char *a_
, unsigned int _a_high
)
295 /* make a local copy of each unbounded array. */
296 memcpy (a
, a_
, _a_high
+1);
298 l
= StrLib_StrLen ((const char *) a
, _a_high
);
302 if ((PushBackInput_PutCh (a
[l
])) != a
[l
])
304 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutString", 9, 132);
311 PutStr - pushes a dynamic string onto the push back stack.
312 The string, s, is not deallocated.
315 extern "C" void PushBackInput_PutStr (DynamicStrings_String s
)
319 i
= DynamicStrings_Length (s
);
323 if ((PushBackInput_PutCh (DynamicStrings_char (s
, static_cast<int> (i
)))) != (DynamicStrings_char (s
, static_cast<int> (i
))))
325 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutStr", 6, 113);
332 Error - emits an error message with the appropriate file, line combination.
335 extern "C" void PushBackInput_Error (const char *a_
, unsigned int _a_high
)
339 /* make a local copy of each unbounded array. */
340 memcpy (a
, a_
, _a_high
+1);
342 StdIO_PushOutput ((StdIO_ProcWrite
) {(StdIO_ProcWrite_t
) ErrChar
});
343 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
345 NumberIO_WriteCard (LineNo
, 0);
347 StrIO_WriteString ((const char *) a
, _a_high
);
350 FIO_Close (FIO_StdErr
);
356 WarnError - emits an error message with the appropriate file, line combination.
357 It does not terminate but when the program finishes an exit status of
361 extern "C" void PushBackInput_WarnError (const char *a_
, unsigned int _a_high
)
365 /* make a local copy of each unbounded array. */
366 memcpy (a
, a_
, _a_high
+1);
368 StdIO_PushOutput ((StdIO_ProcWrite
) {(StdIO_ProcWrite_t
) ErrChar
});
369 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
371 NumberIO_WriteCard (LineNo
, 0);
373 StrIO_WriteString ((const char *) a
, _a_high
);
381 WarnString - emits an error message with the appropriate file, line combination.
382 It does not terminate but when the program finishes an exit status of
386 extern "C" void PushBackInput_WarnString (DynamicStrings_String s
)
388 typedef char *WarnString__T1
;
392 p
= static_cast<WarnString__T1
> (DynamicStrings_string (s
));
393 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
395 NumberIO_WriteCard (LineNo
, 0);
400 if ((*p
) == ASCII_lf
)
403 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
405 NumberIO_WriteCard (LineNo
, 0);
414 } while (! ((p
== NULL
) || ((*p
) == ASCII_nul
)));
420 Close - closes the opened file.
423 extern "C" void PushBackInput_Close (FIO_File f
)
430 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
433 extern "C" unsigned int PushBackInput_GetExitStatus (void)
436 /* static analysis guarentees a RETURN statement will be used before here. */
437 __builtin_unreachable ();
442 SetDebug - sets the debug flag on or off.
445 extern "C" void PushBackInput_SetDebug (bool d
)
452 GetColumnPosition - returns the column position of the current character.
455 extern "C" unsigned int PushBackInput_GetColumnPosition (void)
457 if (StackPtr
> Column
)
463 return Column
-StackPtr
;
465 /* static analysis guarentees a RETURN statement will be used before here. */
466 __builtin_unreachable ();
471 GetCurrentLine - returns the current line number.
474 extern "C" unsigned int PushBackInput_GetCurrentLine (void)
477 /* static analysis guarentees a RETURN statement will be used before here. */
478 __builtin_unreachable ();
481 extern "C" void _M2_PushBackInput_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
483 PushBackInput_SetDebug (false);
487 extern "C" void _M2_PushBackInput_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])