| 1 |
/* Selective.c provide access to timeval and select. |
| 2 |
|
| 3 |
Copyright (C) 2009-2022 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 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 |
#define EXPORT(FUNC) m2pim ## _Selective_ ## FUNC |
| 31 |
#define M2EXPORT(FUNC) m2pim ## _M2_Selective_ ## FUNC |
| 32 |
#define M2LIBNAME "m2pim" |
| 33 |
|
| 34 |
#if defined(HAVE_STDDEF_H) |
| 35 |
/* Obtain a definition for NULL. */ |
| 36 |
#include <stddef.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(HAVE_STDIO_H) |
| 40 |
/* Obtain a definition for NULL. */ |
| 41 |
#include <stdio.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#if defined(HAVE_SYS_TIME_H) |
| 45 |
#include <sys/time.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#if defined(HAVE_TIME_H) |
| 49 |
/* Obtain a definition for NULL. */ |
| 50 |
#include <time.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#if defined(HAVE_STRING_H) |
| 54 |
/* Obtain a definition for NULL. */ |
| 55 |
#include <string.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(HAVE_WCHAR_H) |
| 59 |
/* Obtain a definition for NULL. */ |
| 60 |
#include <wchar.h> |
| 61 |
#endif |
| 62 |
|
| 63 |
#if defined(HAVE_STDLIB_H) |
| 64 |
/* Obtain a prototype for free and malloc. */ |
| 65 |
#include <stdlib.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#if defined(HAVE_SYS_TYPES_H) |
| 69 |
#include <sys/types.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
#if defined(HAVE_UNISTD_H) |
| 73 |
#include <unistd.h> |
| 74 |
#endif |
| 75 |
|
| 76 |
#if !defined(NULL) |
| 77 |
#define NULL (void *)0 |
| 78 |
#endif |
| 79 |
|
| 80 |
#if defined(HAVE_SELECT) |
| 81 |
#define FDSET_T fd_set |
| 82 |
#else |
| 83 |
#define FDSET_T void |
| 84 |
#endif |
| 85 |
|
| 86 |
/* Select wrap a call to the C select. */ |
| 87 |
|
| 88 |
#if defined(HAVE_STRUCT_TIMEVAL) |
| 89 |
extern "C" int |
| 90 |
EXPORT(Select) (int nooffds, fd_set *readfds, fd_set *writefds, |
| 91 |
fd_set *exceptfds, struct timeval *timeout) |
| 92 |
{ |
| 93 |
return select (nooffds, readfds, writefds, exceptfds, timeout); |
| 94 |
} |
| 95 |
#else |
| 96 |
extern "C" int |
| 97 |
EXPORT(Select) (int nooffds, void *readfds, void *writefds, void *exceptfds, |
| 98 |
void *timeout) |
| 99 |
{ |
| 100 |
return 0; |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
/* InitTime initializes a timeval structure and returns a pointer to it. */ |
| 105 |
|
| 106 |
#if defined(HAVE_STRUCT_TIMEVAL) |
| 107 |
extern "C" struct timeval * |
| 108 |
EXPORT(InitTime) (unsigned int sec, unsigned int usec) |
| 109 |
{ |
| 110 |
struct timeval *t = (struct timeval *)malloc (sizeof (struct timeval)); |
| 111 |
|
| 112 |
t->tv_sec = (long int)sec; |
| 113 |
t->tv_usec = (long int)usec; |
| 114 |
return t; |
| 115 |
} |
| 116 |
|
| 117 |
extern "C" void |
| 118 |
EXPORT(GetTime) (struct timeval *t, unsigned int *sec, unsigned int *usec) |
| 119 |
{ |
| 120 |
*sec = (unsigned int)t->tv_sec; |
| 121 |
*usec = (unsigned int)t->tv_usec; |
| 122 |
} |
| 123 |
|
| 124 |
extern "C" void |
| 125 |
EXPORT(SetTime) (struct timeval *t, unsigned int sec, unsigned int usec) |
| 126 |
{ |
| 127 |
t->tv_sec = sec; |
| 128 |
t->tv_usec = usec; |
| 129 |
} |
| 130 |
|
| 131 |
/* KillTime frees the timeval structure and returns NULL. */ |
| 132 |
|
| 133 |
extern "C" struct timeval * |
| 134 |
EXPORT(KillTime) (struct timeval *t) |
| 135 |
{ |
| 136 |
#if defined(HAVE_STDLIB_H) |
| 137 |
free (t); |
| 138 |
#endif |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
/* InitSet returns a pointer to a FD_SET. */ |
| 143 |
|
| 144 |
extern "C" FDSET_T * |
| 145 |
EXPORT(InitSet) (void) |
| 146 |
{ |
| 147 |
#if defined(HAVE_STDLIB_H) |
| 148 |
FDSET_T *s = (FDSET_T *)malloc (sizeof (FDSET_T)); |
| 149 |
|
| 150 |
return s; |
| 151 |
#else |
| 152 |
return NULL |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
/* KillSet frees the FD_SET and returns NULL. */ |
| 157 |
|
| 158 |
extern "C" FDSET_T * |
| 159 |
EXPORT(KillSet) (FDSET_T *s) |
| 160 |
{ |
| 161 |
#if defined(HAVE_STDLIB_H) |
| 162 |
free (s); |
| 163 |
#endif |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
/* FdZero generate an empty set. */ |
| 168 |
|
| 169 |
extern "C" void |
| 170 |
EXPORT(FdZero) (FDSET_T *s) |
| 171 |
{ |
| 172 |
FD_ZERO (s); |
| 173 |
} |
| 174 |
|
| 175 |
/* FS_Set include an element, fd, into set, s. */ |
| 176 |
|
| 177 |
extern "C" void |
| 178 |
EXPORT(FdSet) (int fd, FDSET_T *s) |
| 179 |
{ |
| 180 |
FD_SET (fd, s); |
| 181 |
} |
| 182 |
|
| 183 |
/* FdClr exclude an element, fd, from the set, s. */ |
| 184 |
|
| 185 |
extern "C" void |
| 186 |
EXPORT(FdClr) (int fd, FDSET_T *s) |
| 187 |
{ |
| 188 |
FD_CLR (fd, s); |
| 189 |
} |
| 190 |
|
| 191 |
/* FdIsSet return TRUE if, fd, is present in set, s. */ |
| 192 |
|
| 193 |
extern "C" int |
| 194 |
EXPORT(FdIsSet) (int fd, FDSET_T *s) |
| 195 |
{ |
| 196 |
return FD_ISSET (fd, s); |
| 197 |
} |
| 198 |
|
| 199 |
/* GetTimeOfDay fills in a record, Timeval, filled in with the |
| 200 |
current system time in seconds and microseconds. |
| 201 |
It returns zero (see man 3p gettimeofday). */ |
| 202 |
|
| 203 |
extern "C" int |
| 204 |
EXPORT(GetTimeOfDay) (struct timeval *t) |
| 205 |
{ |
| 206 |
return gettimeofday (t, NULL); |
| 207 |
} |
| 208 |
#else |
| 209 |
|
| 210 |
extern "C" void * |
| 211 |
EXPORT(InitTime) (unsigned int sec, unsigned int usec) |
| 212 |
{ |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
extern "C" void * |
| 217 |
EXPORT(KillTime) (void *t) |
| 218 |
{ |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
extern "C" void |
| 223 |
EXPORT(GetTime) (void *t, unsigned int *sec, unsigned int *usec) |
| 224 |
{ |
| 225 |
} |
| 226 |
|
| 227 |
extern "C" void |
| 228 |
EXPORT(SetTime) (void *t, unsigned int sec, unsigned int usec) |
| 229 |
{ |
| 230 |
} |
| 231 |
|
| 232 |
extern "C" FDSET_T * |
| 233 |
EXPORT(InitSet) (void) |
| 234 |
{ |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
extern "C" FDSET_T * |
| 239 |
EXPORT(KillSet) (void) |
| 240 |
{ |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
|
| 244 |
extern "C" void |
| 245 |
EXPORT(FdZero) (void *s) |
| 246 |
{ |
| 247 |
} |
| 248 |
|
| 249 |
extern "C" void |
| 250 |
EXPORT(FdSet) (int fd, void *s) |
| 251 |
{ |
| 252 |
} |
| 253 |
|
| 254 |
extern "C" void |
| 255 |
EXPORT(FdClr) (int fd, void *s) |
| 256 |
{ |
| 257 |
} |
| 258 |
|
| 259 |
extern "C" int |
| 260 |
EXPORT(FdIsSet) (int fd, void *s) |
| 261 |
{ |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
extern "C" int |
| 266 |
EXPORT(GetTimeOfDay) (void *t) |
| 267 |
{ |
| 268 |
return -1; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
/* MaxFdsPlusOne returns max (a + 1, b + 1). */ |
| 273 |
|
| 274 |
extern "C" int |
| 275 |
EXPORT(MaxFdsPlusOne) (int a, int b) |
| 276 |
{ |
| 277 |
if (a > b) |
| 278 |
return a + 1; |
| 279 |
else |
| 280 |
return b + 1; |
| 281 |
} |
| 282 |
|
| 283 |
/* WriteCharRaw writes a single character to the file descriptor. */ |
| 284 |
|
| 285 |
extern "C" void |
| 286 |
EXPORT(WriteCharRaw) (int fd, char ch) |
| 287 |
{ |
| 288 |
write (fd, &ch, 1); |
| 289 |
} |
| 290 |
|
| 291 |
/* ReadCharRaw read and return a single char from file descriptor, fd. */ |
| 292 |
|
| 293 |
extern "C" char |
| 294 |
EXPORT(ReadCharRaw) (int fd) |
| 295 |
{ |
| 296 |
char ch; |
| 297 |
|
| 298 |
read (fd, &ch, 1); |
| 299 |
return ch; |
| 300 |
} |
| 301 |
|
| 302 |
/* GNU Modula-2 linking hooks. */ |
| 303 |
|
| 304 |
extern "C" void |
| 305 |
M2EXPORT(init) (int, char **, char **) |
| 306 |
{ |
| 307 |
} |
| 308 |
|
| 309 |
extern "C" void |
| 310 |
M2EXPORT(fini) (int, char **, char **) |
| 311 |
{ |
| 312 |
} |
| 313 |
|
| 314 |
extern "C" void |
| 315 |
M2EXPORT(dep) (void) |
| 316 |
{ |
| 317 |
} |
| 318 |
|
| 319 |
extern "C" void __attribute__((__constructor__)) |
| 320 |
M2EXPORT(ctor) (void) |
| 321 |
{ |
| 322 |
m2pim_M2RTS_RegisterModule ("Selective", M2LIBNAME, |
| 323 |
M2EXPORT(init), M2EXPORT(fini), |
| 324 |
M2EXPORT(dep)); |
| 325 |
} |