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/>. */
29 # if !defined (PROC_D)
31 typedef void (*PROC_t
) (void);
32 typedef struct { PROC_t proc
; } PROC
;
42 #if defined(__cplusplus)
46 #define _PushBackInput_H
47 #define _PushBackInput_C
50 # include "GDynamicStrings.h"
54 # include "GNumberIO.h"
59 # define MaxPushBackStack 8192
60 # define MaxFileName 4096
61 typedef struct PushBackInput__T2_a PushBackInput__T2
;
63 typedef struct PushBackInput__T3_a PushBackInput__T3
;
65 struct PushBackInput__T2_a
{ char array
[MaxFileName
+1]; };
66 struct PushBackInput__T3_a
{ char array
[MaxPushBackStack
+1]; };
67 static PushBackInput__T2 FileName
;
68 static PushBackInput__T3 CharStack
;
69 static unsigned int ExitStatus
;
70 static unsigned int Column
;
71 static unsigned int StackPtr
;
72 static unsigned int LineNo
;
73 static bool Debugging
;
76 Open - opens a file for reading.
79 extern "C" FIO_File
PushBackInput_Open (const char *a_
, unsigned int _a_high
);
82 GetCh - gets a character from either the push back stack or
86 extern "C" char PushBackInput_GetCh (FIO_File f
);
89 PutCh - pushes a character onto the push back stack, it also
90 returns the character which has been pushed.
93 extern "C" char PushBackInput_PutCh (char ch
);
96 PutString - pushes a string onto the push back stack.
99 extern "C" void PushBackInput_PutString (const char *a_
, unsigned int _a_high
);
102 PutStr - pushes a dynamic string onto the push back stack.
103 The string, s, is not deallocated.
106 extern "C" void PushBackInput_PutStr (DynamicStrings_String s
);
109 Error - emits an error message with the appropriate file, line combination.
112 extern "C" void PushBackInput_Error (const char *a_
, unsigned int _a_high
);
115 WarnError - emits an error message with the appropriate file, line combination.
116 It does not terminate but when the program finishes an exit status of
120 extern "C" void PushBackInput_WarnError (const char *a_
, unsigned int _a_high
);
123 WarnString - emits an error message with the appropriate file, line combination.
124 It does not terminate but when the program finishes an exit status of
128 extern "C" void PushBackInput_WarnString (DynamicStrings_String s
);
131 Close - closes the opened file.
134 extern "C" void PushBackInput_Close (FIO_File f
);
137 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
140 extern "C" unsigned int PushBackInput_GetExitStatus (void);
143 SetDebug - sets the debug flag on or off.
146 extern "C" void PushBackInput_SetDebug (bool d
);
149 GetColumnPosition - returns the column position of the current character.
152 extern "C" unsigned int PushBackInput_GetColumnPosition (void);
155 GetCurrentLine - returns the current line number.
158 extern "C" unsigned int PushBackInput_GetCurrentLine (void);
161 ErrChar - writes a char, ch, to stderr.
164 static void ErrChar (char ch
);
167 Init - initialize global variables.
170 static void Init (void);
174 ErrChar - writes a char, ch, to stderr.
177 static void ErrChar (char ch
)
179 FIO_WriteChar (FIO_StdErr
, ch
);
184 Init - initialize global variables.
187 static void Init (void)
197 Open - opens a file for reading.
200 extern "C" FIO_File
PushBackInput_Open (const char *a_
, unsigned int _a_high
)
204 /* make a local copy of each unbounded array. */
205 memcpy (a
, a_
, _a_high
+1);
208 StrLib_StrCopy ((const char *) a
, _a_high
, (char *) &FileName
.array
[0], MaxFileName
);
209 return FIO_OpenToRead ((const char *) a
, _a_high
);
210 /* static analysis guarentees a RETURN statement will be used before here. */
211 __builtin_unreachable ();
216 GetCh - gets a character from either the push back stack or
220 extern "C" char PushBackInput_GetCh (FIO_File f
)
229 StdIO_Write (CharStack
.array
[StackPtr
]);
231 return CharStack
.array
[StackPtr
];
235 if ((FIO_EOF (f
)) || (! (FIO_IsNoError (f
))))
242 ch
= FIO_ReadChar (f
);
243 } while (! (((ch
!= ASCII_cr
) || (FIO_EOF (f
))) || (! (FIO_IsNoError (f
)))));
260 /* static analysis guarentees a RETURN statement will be used before here. */
261 __builtin_unreachable ();
266 PutCh - pushes a character onto the push back stack, it also
267 returns the character which has been pushed.
270 extern "C" char PushBackInput_PutCh (char ch
)
272 if (StackPtr
< MaxPushBackStack
)
274 CharStack
.array
[StackPtr
] = ch
;
279 Debug_Halt ((const char *) "max push back stack exceeded, increase MaxPushBackStack", 55, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/PushBackInput.mod", 54, (const char *) "PutCh", 5, 151);
282 /* static analysis guarentees a RETURN statement will be used before here. */
283 __builtin_unreachable ();
288 PutString - pushes a string onto the push back stack.
291 extern "C" void PushBackInput_PutString (const char *a_
, unsigned int _a_high
)
296 /* make a local copy of each unbounded array. */
297 memcpy (a
, a_
, _a_high
+1);
299 l
= StrLib_StrLen ((const char *) a
, _a_high
);
303 if ((PushBackInput_PutCh (a
[l
])) != a
[l
])
305 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/PushBackInput.mod", 54, (const char *) "PutString", 9, 132);
312 PutStr - pushes a dynamic string onto the push back stack.
313 The string, s, is not deallocated.
316 extern "C" void PushBackInput_PutStr (DynamicStrings_String s
)
320 i
= DynamicStrings_Length (s
);
324 if ((PushBackInput_PutCh (DynamicStrings_char (s
, static_cast<int> (i
)))) != (DynamicStrings_char (s
, static_cast<int> (i
))))
326 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/PushBackInput.mod", 54, (const char *) "PutStr", 6, 113);
333 Error - emits an error message with the appropriate file, line combination.
336 extern "C" void PushBackInput_Error (const char *a_
, unsigned int _a_high
)
340 /* make a local copy of each unbounded array. */
341 memcpy (a
, a_
, _a_high
+1);
343 StdIO_PushOutput ((StdIO_ProcWrite
) {(StdIO_ProcWrite_t
) ErrChar
});
344 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
346 NumberIO_WriteCard (LineNo
, 0);
348 StrIO_WriteString ((const char *) a
, _a_high
);
351 FIO_Close (FIO_StdErr
);
357 WarnError - emits an error message with the appropriate file, line combination.
358 It does not terminate but when the program finishes an exit status of
362 extern "C" void PushBackInput_WarnError (const char *a_
, unsigned int _a_high
)
366 /* make a local copy of each unbounded array. */
367 memcpy (a
, a_
, _a_high
+1);
369 StdIO_PushOutput ((StdIO_ProcWrite
) {(StdIO_ProcWrite_t
) ErrChar
});
370 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
372 NumberIO_WriteCard (LineNo
, 0);
374 StrIO_WriteString ((const char *) a
, _a_high
);
382 WarnString - emits an error message with the appropriate file, line combination.
383 It does not terminate but when the program finishes an exit status of
387 extern "C" void PushBackInput_WarnString (DynamicStrings_String s
)
389 typedef char *WarnString__T1
;
393 p
= static_cast<WarnString__T1
> (DynamicStrings_string (s
));
394 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
396 NumberIO_WriteCard (LineNo
, 0);
401 if ((*p
) == ASCII_lf
)
404 StrIO_WriteString ((const char *) &FileName
.array
[0], MaxFileName
);
406 NumberIO_WriteCard (LineNo
, 0);
415 } while (! ((p
== NULL
) || ((*p
) == ASCII_nul
)));
421 Close - closes the opened file.
424 extern "C" void PushBackInput_Close (FIO_File f
)
431 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
434 extern "C" unsigned int PushBackInput_GetExitStatus (void)
437 /* static analysis guarentees a RETURN statement will be used before here. */
438 __builtin_unreachable ();
443 SetDebug - sets the debug flag on or off.
446 extern "C" void PushBackInput_SetDebug (bool d
)
453 GetColumnPosition - returns the column position of the current character.
456 extern "C" unsigned int PushBackInput_GetColumnPosition (void)
458 if (StackPtr
> Column
)
464 return Column
-StackPtr
;
466 /* static analysis guarentees a RETURN statement will be used before here. */
467 __builtin_unreachable ();
472 GetCurrentLine - returns the current line number.
475 extern "C" unsigned int PushBackInput_GetCurrentLine (void)
478 /* static analysis guarentees a RETURN statement will be used before here. */
479 __builtin_unreachable ();
482 extern "C" void _M2_PushBackInput_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
484 PushBackInput_SetDebug (false);
488 extern "C" void _M2_PushBackInput_fini (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])