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