]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgm2/libm2pim/Selective.cc
Merge modula-2 front end onto gcc.
[thirdparty/gcc.git] / libgm2 / libm2pim / Selective.cc
1 /* Selective.c provide access to timeval and select.
2
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include <config.h>
28 #include <m2rts.h>
29
30 #if defined(HAVE_STDDEF_H)
31 /* Obtain a definition for NULL. */
32 #include <stddef.h>
33 #endif
34
35 #if defined(HAVE_STDIO_H)
36 /* Obtain a definition for NULL. */
37 #include <stdio.h>
38 #endif
39
40 #if defined(HAVE_SYS_TIME_H)
41 #include <sys/time.h>
42 #endif
43
44 #if defined(HAVE_TIME_H)
45 /* Obtain a definition for NULL. */
46 #include <time.h>
47 #endif
48
49 #if defined(HAVE_STRING_H)
50 /* Obtain a definition for NULL. */
51 #include <string.h>
52 #endif
53
54 #if defined(HAVE_WCHAR_H)
55 /* Obtain a definition for NULL. */
56 #include <wchar.h>
57 #endif
58
59 #if defined(HAVE_STDLIB_H)
60 /* Obtain a prototype for free and malloc. */
61 #include <stdlib.h>
62 #endif
63
64 #if defined(HAVE_SYS_TYPES_H)
65 #include <sys/types.h>
66 #endif
67
68 #if defined(HAVE_UNISTD_H)
69 #include <unistd.h>
70 #endif
71
72 #if !defined(NULL)
73 #define NULL (void *)0
74 #endif
75
76 #if defined(HAVE_SELECT)
77 #define FDSET_T fd_set
78 #else
79 #define FDSET_T void
80 #endif
81
82 /* Select wrap a call to the C select. */
83
84 #if defined(HAVE_STRUCT_TIMEVAL)
85 extern "C" int
86 Selective_Select (int nooffds, fd_set *readfds, fd_set *writefds,
87 fd_set *exceptfds, struct timeval *timeout)
88 {
89 return select (nooffds, readfds, writefds, exceptfds, timeout);
90 }
91 #else
92 extern "C" int
93 Selective_Select (int nooffds, void *readfds, void *writefds, void *exceptfds,
94 void *timeout)
95 {
96 return 0;
97 }
98 #endif
99
100 /* InitTime initializes a timeval structure and returns a pointer to it. */
101
102 #if defined(HAVE_STRUCT_TIMEVAL)
103 extern "C" struct timeval *
104 Selective_InitTime (unsigned int sec, unsigned int usec)
105 {
106 struct timeval *t = (struct timeval *)malloc (sizeof (struct timeval));
107
108 t->tv_sec = (long int)sec;
109 t->tv_usec = (long int)usec;
110 return t;
111 }
112
113 extern "C" void
114 Selective_GetTime (struct timeval *t, unsigned int *sec, unsigned int *usec)
115 {
116 *sec = (unsigned int)t->tv_sec;
117 *usec = (unsigned int)t->tv_usec;
118 }
119
120 extern "C" void
121 Selective_SetTime (struct timeval *t, unsigned int sec, unsigned int usec)
122 {
123 t->tv_sec = sec;
124 t->tv_usec = usec;
125 }
126
127 /* KillTime frees the timeval structure and returns NULL. */
128
129 extern "C" struct timeval *
130 Selective_KillTime (struct timeval *t)
131 {
132 #if defined(HAVE_STDLIB_H)
133 free (t);
134 #endif
135 return NULL;
136 }
137
138 /* InitSet returns a pointer to a FD_SET. */
139
140 extern "C" FDSET_T *
141 Selective_InitSet (void)
142 {
143 #if defined(HAVE_STDLIB_H)
144 FDSET_T *s = (FDSET_T *)malloc (sizeof (FDSET_T));
145
146 return s;
147 #else
148 return NULL
149 #endif
150 }
151
152 /* KillSet frees the FD_SET and returns NULL. */
153
154 extern "C" FDSET_T *
155 Selective_KillSet (FDSET_T *s)
156 {
157 #if defined(HAVE_STDLIB_H)
158 free (s);
159 #endif
160 return NULL;
161 }
162
163 /* FdZero generate an empty set. */
164
165 extern "C" void
166 Selective_FdZero (FDSET_T *s)
167 {
168 FD_ZERO (s);
169 }
170
171 /* FS_Set include an element, fd, into set, s. */
172
173 extern "C" void
174 Selective_FdSet (int fd, FDSET_T *s)
175 {
176 FD_SET (fd, s);
177 }
178
179 /* FdClr exclude an element, fd, from the set, s. */
180
181 extern "C" void
182 Selective_FdClr (int fd, FDSET_T *s)
183 {
184 FD_CLR (fd, s);
185 }
186
187 /* FdIsSet return TRUE if, fd, is present in set, s. */
188
189 extern "C" int
190 Selective_FdIsSet (int fd, FDSET_T *s)
191 {
192 return FD_ISSET (fd, s);
193 }
194
195 /* GetTimeOfDay fills in a record, Timeval, filled in with the
196 current system time in seconds and microseconds.
197 It returns zero (see man 3p gettimeofday). */
198
199 extern "C" int
200 Selective_GetTimeOfDay (struct timeval *t)
201 {
202 return gettimeofday (t, NULL);
203 }
204 #else
205
206 extern "C" void *
207 Selective_InitTime (unsigned int sec, unsigned int usec)
208 {
209 return NULL;
210 }
211
212 extern "C" void *
213 Selective_KillTime (void *t)
214 {
215 return NULL;
216 }
217
218 extern "C" void
219 Selective_GetTime (void *t, unsigned int *sec, unsigned int *usec)
220 {
221 }
222
223 extern "C" void
224 Selective_SetTime (void *t, unsigned int sec, unsigned int usec)
225 {
226 }
227
228 extern "C" FDSET_T *
229 Selective_InitSet (void)
230 {
231 return NULL;
232 }
233
234 extern "C" FDSET_T *
235 Selective_KillSet (void)
236 {
237 return NULL;
238 }
239
240 extern "C" void
241 Selective_FdZero (void *s)
242 {
243 }
244
245 extern "C" void
246 Selective_FdSet (int fd, void *s)
247 {
248 }
249
250 extern "C" void
251 Selective_FdClr (int fd, void *s)
252 {
253 }
254
255 extern "C" int
256 Selective_FdIsSet (int fd, void *s)
257 {
258 return 0;
259 }
260
261 extern "C" int
262 Selective_GetTimeOfDay (void *t)
263 {
264 return -1;
265 }
266 #endif
267
268 /* MaxFdsPlusOne returns max (a + 1, b + 1). */
269
270 extern "C" int
271 Selective_MaxFdsPlusOne (int a, int b)
272 {
273 if (a > b)
274 return a + 1;
275 else
276 return b + 1;
277 }
278
279 /* WriteCharRaw writes a single character to the file descriptor. */
280
281 extern "C" void
282 Selective_WriteCharRaw (int fd, char ch)
283 {
284 write (fd, &ch, 1);
285 }
286
287 /* ReadCharRaw read and return a single char from file descriptor, fd. */
288
289 extern "C" char
290 Selective_ReadCharRaw (int fd)
291 {
292 char ch;
293
294 read (fd, &ch, 1);
295 return ch;
296 }
297
298 extern "C" void
299 _M2_Selective_init (int argc, char *argv[], char *envp[])
300 {
301 }
302
303 extern "C" void
304 _M2_Selective_fini (int argc, char *argv[], char *envp[])
305 {
306 }
307
308 extern "C" void
309 _M2_Selective_dep (void)
310 {
311 }
312
313 struct _M2_Selective_ctor { _M2_Selective_ctor (); } _M2_Selective_ctor;
314
315 _M2_Selective_ctor::_M2_Selective_ctor (void)
316 {
317 M2RTS_RegisterModule ("Selective", _M2_Selective_init, _M2_Selective_fini,
318 _M2_Selective_dep);
319 }