Debian Codegraph @

gcc/m2/mc-boot/GPushBackInput.cc

raw
1 /* do not edit automatically generated by mc from PushBackInput. */
2 /* PushBackInput.mod provides a method for pushing back and consuming input.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
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 #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
36
37 # if !defined (FALSE)
38 # define FALSE (1==0)
39 # endif
40
41 #if defined(__cplusplus)
42 # undef NULL
43 # define NULL 0
44 #endif
45 #define _PushBackInput_C
46
47 #include "GPushBackInput.h"
48 # include "GFIO.h"
49 # include "GDynamicStrings.h"
50 # include "GASCII.h"
51 # include "GDebug.h"
52 # include "GStrLib.h"
53 # include "GNumberIO.h"
54 # include "GStrIO.h"
55 # include "GStdIO.h"
56 # include "Glibc.h"
57
58 # define MaxPushBackStack 8192
59 # define MaxFileName 4096
60 typedef struct PushBackInput__T2_a PushBackInput__T2;
61
62 typedef struct PushBackInput__T3_a PushBackInput__T3;
63
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;
73
74 /*
75 Open - opens a file for reading.
76 */
77
78 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high);
79
80 /*
81 GetCh - gets a character from either the push back stack or
82 from file, f.
83 */
84
85 extern "C" char PushBackInput_GetCh (FIO_File f);
86
87 /*
88 PutCh - pushes a character onto the push back stack, it also
89 returns the character which has been pushed.
90 */
91
92 extern "C" char PushBackInput_PutCh (char ch);
93
94 /*
95 PutString - pushes a string onto the push back stack.
96 */
97
98 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high);
99
100 /*
101 PutStr - pushes a dynamic string onto the push back stack.
102 The string, s, is not deallocated.
103 */
104
105 extern "C" void PushBackInput_PutStr (DynamicStrings_String s);
106
107 /*
108 Error - emits an error message with the appropriate file, line combination.
109 */
110
111 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high);
112
113 /*
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
116 1 will be issued.
117 */
118
119 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high);
120
121 /*
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
124 1 will be issued.
125 */
126
127 extern "C" void PushBackInput_WarnString (DynamicStrings_String s);
128
129 /*
130 Close - closes the opened file.
131 */
132
133 extern "C" void PushBackInput_Close (FIO_File f);
134
135 /*
136 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
137 */
138
139 extern "C" unsigned int PushBackInput_GetExitStatus (void);
140
141 /*
142 SetDebug - sets the debug flag on or off.
143 */
144
145 extern "C" void PushBackInput_SetDebug (bool d);
146
147 /*
148 GetColumnPosition - returns the column position of the current character.
149 */
150
151 extern "C" unsigned int PushBackInput_GetColumnPosition (void);
152
153 /*
154 GetCurrentLine - returns the current line number.
155 */
156
157 extern "C" unsigned int PushBackInput_GetCurrentLine (void);
158
159 /*
160 ErrChar - writes a char, ch, to stderr.
161 */
162
163 static void ErrChar (char ch);
164
165 /*
166 Init - initialize global variables.
167 */
168
169 static void Init (void);
170
171
172 /*
173 ErrChar - writes a char, ch, to stderr.
174 */
175
176 static void ErrChar (char ch)
177 {
178 FIO_WriteChar (FIO_StdErr, ch);
179 }
180
181
182 /*
183 Init - initialize global variables.
184 */
185
186 static void Init (void)
187 {
188 ExitStatus = 0;
189 StackPtr = 0;
190 LineNo = 1;
191 Column = 0;
192 }
193
194
195 /*
196 Open - opens a file for reading.
197 */
198
199 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high)
200 {
201 char a[_a_high+1];
202
203 /* make a local copy of each unbounded array. */
204 memcpy (a, a_, _a_high+1);
205
206 Init ();
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 ();
211 }
212
213
214 /*
215 GetCh - gets a character from either the push back stack or
216 from file, f.
217 */
218
219 extern "C" char PushBackInput_GetCh (FIO_File f)
220 {
221 char ch;
222
223 if (StackPtr > 0)
224 {
225 StackPtr -= 1;
226 if (Debugging)
227 {
228 StdIO_Write (CharStack.array[StackPtr]);
229 }
230 return CharStack.array[StackPtr];
231 }
232 else
233 {
234 if ((FIO_EOF (f)) || (! (FIO_IsNoError (f))))
235 {
236 ch = ASCII_nul;
237 }
238 else
239 {
240 do {
241 ch = FIO_ReadChar (f);
242 } while (! (((ch != ASCII_cr) || (FIO_EOF (f))) || (! (FIO_IsNoError (f)))));
243 if (ch == ASCII_lf)
244 {
245 Column = 0;
246 LineNo += 1;
247 }
248 else
249 {
250 Column += 1;
251 }
252 }
253 if (Debugging)
254 {
255 StdIO_Write (ch);
256 }
257 return ch;
258 }
259 /* static analysis guarentees a RETURN statement will be used before here. */
260 __builtin_unreachable ();
261 }
262
263
264 /*
265 PutCh - pushes a character onto the push back stack, it also
266 returns the character which has been pushed.
267 */
268
269 extern "C" char PushBackInput_PutCh (char ch)
270 {
271 if (StackPtr < MaxPushBackStack)
272 {
273 CharStack.array[StackPtr] = ch;
274 StackPtr += 1;
275 }
276 else
277 {
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);
279 }
280 return ch;
281 /* static analysis guarentees a RETURN statement will be used before here. */
282 __builtin_unreachable ();
283 }
284
285
286 /*
287 PutString - pushes a string onto the push back stack.
288 */
289
290 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high)
291 {
292 unsigned int l;
293 char a[_a_high+1];
294
295 /* make a local copy of each unbounded array. */
296 memcpy (a, a_, _a_high+1);
297
298 l = StrLib_StrLen ((const char *) a, _a_high);
299 while (l > 0)
300 {
301 l -= 1;
302 if ((PushBackInput_PutCh (a[l])) != a[l])
303 {
304 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutString", 9, 132);
305 }
306 }
307 }
308
309
310 /*
311 PutStr - pushes a dynamic string onto the push back stack.
312 The string, s, is not deallocated.
313 */
314
315 extern "C" void PushBackInput_PutStr (DynamicStrings_String s)
316 {
317 unsigned int i;
318
319 i = DynamicStrings_Length (s);
320 while (i > 0)
321 {
322 i -= 1;
323 if ((PushBackInput_PutCh (DynamicStrings_char (s, static_cast<int> (i)))) != (DynamicStrings_char (s, static_cast<int> (i))))
324 {
325 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutStr", 6, 113);
326 }
327 }
328 }
329
330
331 /*
332 Error - emits an error message with the appropriate file, line combination.
333 */
334
335 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high)
336 {
337 char a[_a_high+1];
338
339 /* make a local copy of each unbounded array. */
340 memcpy (a, a_, _a_high+1);
341
342 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
343 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
344 StdIO_Write (':');
345 NumberIO_WriteCard (LineNo, 0);
346 StdIO_Write (':');
347 StrIO_WriteString ((const char *) a, _a_high);
348 StrIO_WriteLn ();
349 StdIO_PopOutput ();
350 FIO_Close (FIO_StdErr);
351 libc_exit (1);
352 }
353
354
355 /*
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
358 1 will be issued.
359 */
360
361 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high)
362 {
363 char a[_a_high+1];
364
365 /* make a local copy of each unbounded array. */
366 memcpy (a, a_, _a_high+1);
367
368 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
369 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
370 StdIO_Write (':');
371 NumberIO_WriteCard (LineNo, 0);
372 StdIO_Write (':');
373 StrIO_WriteString ((const char *) a, _a_high);
374 StrIO_WriteLn ();
375 StdIO_PopOutput ();
376 ExitStatus = 1;
377 }
378
379
380 /*
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
383 1 will be issued.
384 */
385
386 extern "C" void PushBackInput_WarnString (DynamicStrings_String s)
387 {
388 typedef char *WarnString__T1;
389
390 WarnString__T1 p;
391
392 p = static_cast<WarnString__T1> (DynamicStrings_string (s));
393 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
394 StdIO_Write (':');
395 NumberIO_WriteCard (LineNo, 0);
396 StdIO_Write (':');
397 do {
398 if (p != NULL)
399 {
400 if ((*p) == ASCII_lf)
401 {
402 StrIO_WriteLn ();
403 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
404 StdIO_Write (':');
405 NumberIO_WriteCard (LineNo, 0);
406 StdIO_Write (':');
407 }
408 else
409 {
410 StdIO_Write ((*p));
411 }
412 p += 1;
413 }
414 } while (! ((p == NULL) || ((*p) == ASCII_nul)));
415 ExitStatus = 1;
416 }
417
418
419 /*
420 Close - closes the opened file.
421 */
422
423 extern "C" void PushBackInput_Close (FIO_File f)
424 {
425 FIO_Close (f);
426 }
427
428
429 /*
430 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
431 */
432
433 extern "C" unsigned int PushBackInput_GetExitStatus (void)
434 {
435 return ExitStatus;
436 /* static analysis guarentees a RETURN statement will be used before here. */
437 __builtin_unreachable ();
438 }
439
440
441 /*
442 SetDebug - sets the debug flag on or off.
443 */
444
445 extern "C" void PushBackInput_SetDebug (bool d)
446 {
447 Debugging = d;
448 }
449
450
451 /*
452 GetColumnPosition - returns the column position of the current character.
453 */
454
455 extern "C" unsigned int PushBackInput_GetColumnPosition (void)
456 {
457 if (StackPtr > Column)
458 {
459 return 0;
460 }
461 else
462 {
463 return Column-StackPtr;
464 }
465 /* static analysis guarentees a RETURN statement will be used before here. */
466 __builtin_unreachable ();
467 }
468
469
470 /*
471 GetCurrentLine - returns the current line number.
472 */
473
474 extern "C" unsigned int PushBackInput_GetCurrentLine (void)
475 {
476 return LineNo;
477 /* static analysis guarentees a RETURN statement will be used before here. */
478 __builtin_unreachable ();
479 }
480
481 extern "C" void _M2_PushBackInput_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
482 {
483 PushBackInput_SetDebug (false);
484 Init ();
485 }
486
487 extern "C" void _M2_PushBackInput_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
488 {
489 }