Debian Codegraph @

gcc/m2/pge-boot/GOutput.cc

raw
1 /* do not edit automatically generated by mc from Output. */
2 /* Output.mod redirect output.
3
4 Copyright (C) 2021-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 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include <stdbool.h>
24 # if !defined (PROC_D)
25 # define PROC_D
26 typedef void (*PROC_t) (void);
27 typedef struct { PROC_t proc; } PROC;
28 # endif
29
30 # if !defined (TRUE)
31 # define TRUE (1==1)
32 # endif
33
34 # if !defined (FALSE)
35 # define FALSE (1==0)
36 # endif
37
38 #include <stddef.h>
39 #include <string.h>
40 #include <limits.h>
41 #if defined(__cplusplus)
42 # undef NULL
43 # define NULL 0
44 #endif
45 #define _Output_C
46
47 #include "GOutput.h"
48 # include "GFIO.h"
49 # include "GSFIO.h"
50 # include "GStrLib.h"
51 # include "GNameKey.h"
52 # include "GNumberIO.h"
53 # include "GASCII.h"
54 # include "GDynamicStrings.h"
55
56 static bool stdout_;
57 static FIO_File outputFile;
58 static DynamicStrings_String buffer;
59
60 /*
61 Open - attempt to open filename as the output file.
62 TRUE is returned if success, FALSE otherwise.
63 */
64
65 extern "C" bool Output_Open (const char *filename_, unsigned int _filename_high);
66
67 /*
68 Close - close the output file.
69 */
70
71 extern "C" void Output_Close (void);
72
73 /*
74 Write - write a single character to the output file.
75 */
76
77 extern "C" void Output_Write (char ch);
78
79 /*
80 WriteString - write an unformatted string to the output.
81 */
82
83 extern "C" void Output_WriteString (const char *s_, unsigned int _s_high);
84
85 /*
86 KillWriteS - write a string to the output and free the string afterwards.
87 */
88
89 extern "C" void Output_KillWriteS (DynamicStrings_String s);
90
91 /*
92 WriteS - write a string to the output. The string is not freed.
93 */
94
95 extern "C" void Output_WriteS (DynamicStrings_String s);
96
97 /*
98 WriteKey - write a key to the output.
99 */
100
101 extern "C" void Output_WriteKey (NameKey_Name key);
102
103 /*
104 WriteLn - write a newline to the output.
105 */
106
107 extern "C" void Output_WriteLn (void);
108
109 /*
110 WriteCard - write a cardinal using fieldlength characters.
111 */
112
113 extern "C" void Output_WriteCard (unsigned int card, unsigned int fieldlength);
114
115 /*
116 StartBuffer - create a buffer into which any output is redirected.
117 */
118
119 extern "C" void Output_StartBuffer (void);
120
121 /*
122 EndBuffer - end the redirection and return the contents of the buffer.
123 */
124
125 extern "C" DynamicStrings_String Output_EndBuffer (void);
126
127
128 /*
129 Open - attempt to open filename as the output file.
130 TRUE is returned if success, FALSE otherwise.
131 */
132
133 extern "C" bool Output_Open (const char *filename_, unsigned int _filename_high)
134 {
135 char filename[_filename_high+1];
136
137 /* make a local copy of each unbounded array. */
138 memcpy (filename, filename_, _filename_high+1);
139
140 if ((StrLib_StrEqual ((const char *) filename, _filename_high, (const char *) "<stdout>", 8)) || (StrLib_StrEqual ((const char *) filename, _filename_high, (const char *) "-", 1)))
141 {
142 outputFile = FIO_StdOut;
143 stdout_ = true;
144 return true;
145 }
146 else
147 {
148 outputFile = FIO_OpenToWrite ((const char *) filename, _filename_high);
149 stdout_ = false;
150 return FIO_IsNoError (outputFile);
151 }
152 /* static analysis guarentees a RETURN statement will be used before here. */
153 __builtin_unreachable ();
154 }
155
156
157 /*
158 Close - close the output file.
159 */
160
161 extern "C" void Output_Close (void)
162 {
163 FIO_Close (outputFile);
164 }
165
166
167 /*
168 Write - write a single character to the output file.
169 */
170
171 extern "C" void Output_Write (char ch)
172 {
173 if (buffer == NULL)
174 {
175 FIO_WriteChar (outputFile, ch);
176 }
177 else
178 {
179 buffer = DynamicStrings_ConCatChar (buffer, ch);
180 }
181 }
182
183
184 /*
185 WriteString - write an unformatted string to the output.
186 */
187
188 extern "C" void Output_WriteString (const char *s_, unsigned int _s_high)
189 {
190 char s[_s_high+1];
191
192 /* make a local copy of each unbounded array. */
193 memcpy (s, s_, _s_high+1);
194
195 if (buffer == NULL)
196 {
197 FIO_WriteString (outputFile, (const char *) s, _s_high);
198 }
199 else
200 {
201 buffer = DynamicStrings_ConCat (buffer, DynamicStrings_Mark (DynamicStrings_InitString ((const char *) s, _s_high)));
202 }
203 }
204
205
206 /*
207 KillWriteS - write a string to the output and free the string afterwards.
208 */
209
210 extern "C" void Output_KillWriteS (DynamicStrings_String s)
211 {
212 if ((DynamicStrings_KillString (SFIO_WriteS (outputFile, s))) == NULL)
213 {} /* empty. */
214 }
215
216
217 /*
218 WriteS - write a string to the output. The string is not freed.
219 */
220
221 extern "C" void Output_WriteS (DynamicStrings_String s)
222 {
223 if ((SFIO_WriteS (outputFile, s)) == s)
224 {} /* empty. */
225 }
226
227
228 /*
229 WriteKey - write a key to the output.
230 */
231
232 extern "C" void Output_WriteKey (NameKey_Name key)
233 {
234 if (buffer == NULL)
235 {
236 Output_KillWriteS (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (key)));
237 }
238 else
239 {
240 buffer = DynamicStrings_ConCat (buffer, DynamicStrings_Mark (DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (key))));
241 }
242 }
243
244
245 /*
246 WriteLn - write a newline to the output.
247 */
248
249 extern "C" void Output_WriteLn (void)
250 {
251 if (buffer == NULL)
252 {
253 FIO_WriteLine (outputFile);
254 }
255 else
256 {
257 Output_Write (ASCII_nl);
258 }
259 }
260
261
262 /*
263 WriteCard - write a cardinal using fieldlength characters.
264 */
265
266 extern "C" void Output_WriteCard (unsigned int card, unsigned int fieldlength)
267 {
268 typedef struct WriteCard__T1_a WriteCard__T1;
269
270 struct WriteCard__T1_a { char array[20+1]; };
271 WriteCard__T1 s;
272
273 NumberIO_CardToStr (card, fieldlength, (char *) &s.array[0], 20);
274 Output_WriteString ((const char *) &s.array[0], 20);
275 }
276
277
278 /*
279 StartBuffer - create a buffer into which any output is redirected.
280 */
281
282 extern "C" void Output_StartBuffer (void)
283 {
284 if (buffer != NULL)
285 {
286 buffer = DynamicStrings_KillString (buffer);
287 }
288 buffer = DynamicStrings_InitString ((const char *) "", 0);
289 }
290
291
292 /*
293 EndBuffer - end the redirection and return the contents of the buffer.
294 */
295
296 extern "C" DynamicStrings_String Output_EndBuffer (void)
297 {
298 DynamicStrings_String s;
299
300 s = buffer;
301 buffer = static_cast<DynamicStrings_String> (NULL);
302 return s;
303 /* static analysis guarentees a RETURN statement will be used before here. */
304 __builtin_unreachable ();
305 }
306
307 extern "C" void _M2_Output_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
308 {
309 stdout_ = true;
310 buffer = static_cast<DynamicStrings_String> (NULL);
311 outputFile = FIO_StdOut;
312 }
313
314 extern "C" void _M2_Output_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
315 {
316 }