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