| 1 |
/* GSelective.c provides access to select for Modula-2. |
| 2 |
|
| 3 |
Copyright (C) 2016-2025 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 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
/* implementation module in C. */ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include "system.h" |
| 26 |
#include "ansidecl.h" |
| 27 |
|
| 28 |
#include "gm2-libs-host.h" |
| 29 |
|
| 30 |
#if defined(__cplusplus) |
| 31 |
#define EXTERN extern "C" |
| 32 |
#else |
| 33 |
#define EXTERN |
| 34 |
#endif |
| 35 |
|
| 36 |
/* PROCEDURE Select (nooffds: CARDINAL; readfds, writefds, exceptfds: |
| 37 |
SetOfFd; timeout: Timeval) : INTEGER ; */ |
| 38 |
|
| 39 |
#if defined(HAVE_SELECT) |
| 40 |
EXTERN |
| 41 |
int |
| 42 |
Selective_Select (int nooffds, fd_set *readfds, fd_set *writefds, |
| 43 |
fd_set *exceptfds, struct timeval *timeout) |
| 44 |
{ |
| 45 |
return select (nooffds, readfds, writefds, exceptfds, timeout); |
| 46 |
} |
| 47 |
#else |
| 48 |
EXTERN |
| 49 |
int |
| 50 |
Selective_Select (int nooffds, void *readfds, void *writefds, void *exceptfds, |
| 51 |
void *timeout) |
| 52 |
{ |
| 53 |
return 0; |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
/* PROCEDURE InitTime (sec, usec) : Timeval ; */ |
| 58 |
|
| 59 |
#if defined(HAVE_SELECT) |
| 60 |
EXTERN |
| 61 |
struct timeval * |
| 62 |
Selective_InitTime (unsigned int sec, unsigned int usec) |
| 63 |
{ |
| 64 |
struct timeval *t = (struct timeval *)malloc (sizeof (struct timeval)); |
| 65 |
|
| 66 |
t->tv_sec = (long int)sec; |
| 67 |
t->tv_usec = (long int)usec; |
| 68 |
return t; |
| 69 |
} |
| 70 |
|
| 71 |
EXTERN |
| 72 |
void |
| 73 |
Selective_GetTime (struct timeval *t, unsigned int *sec, unsigned int *usec) |
| 74 |
{ |
| 75 |
*sec = (unsigned int)t->tv_sec; |
| 76 |
*usec = (unsigned int)t->tv_usec; |
| 77 |
} |
| 78 |
|
| 79 |
EXTERN |
| 80 |
void |
| 81 |
Selective_SetTime (struct timeval *t, unsigned int sec, unsigned int usec) |
| 82 |
{ |
| 83 |
t->tv_sec = sec; |
| 84 |
t->tv_usec = usec; |
| 85 |
} |
| 86 |
|
| 87 |
/* PROCEDURE KillTime (t: Timeval) : Timeval ; */ |
| 88 |
|
| 89 |
EXTERN |
| 90 |
struct timeval * |
| 91 |
Selective_KillTime (struct timeval *t) |
| 92 |
{ |
| 93 |
free (t); |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
/* PROCEDURE InitSet () : SetOfFd ; */ |
| 98 |
|
| 99 |
EXTERN |
| 100 |
fd_set * |
| 101 |
Selective_InitSet (void) |
| 102 |
{ |
| 103 |
fd_set *s = (fd_set *)malloc (sizeof (fd_set)); |
| 104 |
|
| 105 |
return s; |
| 106 |
} |
| 107 |
|
| 108 |
/* PROCEDURE KillSet (s: SetOfFd) : SetOfFd ; */ |
| 109 |
|
| 110 |
EXTERN |
| 111 |
fd_set * |
| 112 |
Selective_KillSet (fd_set *s) |
| 113 |
{ |
| 114 |
free (s); |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
/* PROCEDURE FdZero (s: SetOfFd) ; */ |
| 119 |
|
| 120 |
EXTERN |
| 121 |
void |
| 122 |
Selective_FdZero (fd_set *s) |
| 123 |
{ |
| 124 |
FD_ZERO (s); |
| 125 |
} |
| 126 |
|
| 127 |
/* PROCEDURE Fd_Set (fd: INTEGER; SetOfFd) ; */ |
| 128 |
|
| 129 |
EXTERN |
| 130 |
void |
| 131 |
Selective_FdSet (int fd, fd_set *s) |
| 132 |
{ |
| 133 |
FD_SET (fd, s); |
| 134 |
} |
| 135 |
|
| 136 |
/* PROCEDURE FdClr (fd: INTEGER; SetOfFd) ; */ |
| 137 |
|
| 138 |
EXTERN |
| 139 |
void |
| 140 |
Selective_FdClr (int fd, fd_set *s) |
| 141 |
{ |
| 142 |
FD_CLR (fd, s); |
| 143 |
} |
| 144 |
|
| 145 |
/* PROCEDURE FdIsSet (fd: INTEGER; SetOfFd) : BOOLEAN ; */ |
| 146 |
|
| 147 |
EXTERN |
| 148 |
bool |
| 149 |
Selective_FdIsSet (int fd, fd_set *s) |
| 150 |
{ |
| 151 |
return FD_ISSET (fd, s); |
| 152 |
} |
| 153 |
|
| 154 |
/* GetTimeOfDay - fills in a record, Timeval, filled in with the |
| 155 |
current system time in seconds and microseconds. It returns zero |
| 156 |
(see man 3p gettimeofday) */ |
| 157 |
|
| 158 |
EXTERN |
| 159 |
int |
| 160 |
Selective_GetTimeOfDay (struct timeval *t) |
| 161 |
{ |
| 162 |
return gettimeofday (t, NULL); |
| 163 |
} |
| 164 |
#else |
| 165 |
|
| 166 |
EXTERN |
| 167 |
void * |
| 168 |
Selective_InitTime (unsigned int sec, unsigned int usec) |
| 169 |
{ |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
EXTERN |
| 174 |
void * |
| 175 |
Selective_KillTime (void *t) |
| 176 |
{ |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
EXTERN |
| 181 |
void |
| 182 |
Selective_GetTime (struct timeval *t, unsigned int *sec, unsigned int *usec) |
| 183 |
{ |
| 184 |
} |
| 185 |
|
| 186 |
EXTERN |
| 187 |
void |
| 188 |
Selective_SetTime (struct timeval *t, unsigned int sec, unsigned int usec) |
| 189 |
{ |
| 190 |
} |
| 191 |
|
| 192 |
EXTERN |
| 193 |
fd_set * |
| 194 |
Selective_InitSet (void) |
| 195 |
{ |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
EXTERN |
| 200 |
void |
| 201 |
Selective_FdZero (void *s) |
| 202 |
{ |
| 203 |
} |
| 204 |
|
| 205 |
EXTERN |
| 206 |
void |
| 207 |
Selective_FdSet (int fd, void *s) |
| 208 |
{ |
| 209 |
} |
| 210 |
|
| 211 |
EXTERN |
| 212 |
void |
| 213 |
Selective_FdClr (int fd, void *s) |
| 214 |
{ |
| 215 |
} |
| 216 |
|
| 217 |
EXTERN |
| 218 |
int |
| 219 |
Selective_FdIsSet (int fd, void *s) |
| 220 |
{ |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
EXTERN |
| 225 |
int |
| 226 |
Selective_GetTimeOfDay (struct timeval *t) |
| 227 |
{ |
| 228 |
return -1; |
| 229 |
} |
| 230 |
#endif |
| 231 |
|
| 232 |
/* PROCEDURE MaxFdsPlusOne (a, b: File) : File ; */ |
| 233 |
|
| 234 |
EXTERN |
| 235 |
int |
| 236 |
Selective_MaxFdsPlusOne (int a, int b) |
| 237 |
{ |
| 238 |
if (a > b) |
| 239 |
return a + 1; |
| 240 |
else |
| 241 |
return b + 1; |
| 242 |
} |
| 243 |
|
| 244 |
/* PROCEDURE WriteCharRaw (fd: INTEGER; ch: CHAR) ; */ |
| 245 |
|
| 246 |
EXTERN |
| 247 |
void |
| 248 |
Selective_WriteCharRaw (int fd, char ch) |
| 249 |
{ |
| 250 |
write (fd, &ch, 1); |
| 251 |
} |
| 252 |
|
| 253 |
/* PROCEDURE ReadCharRaw (fd: INTEGER) : CHAR ; */ |
| 254 |
|
| 255 |
EXTERN |
| 256 |
char |
| 257 |
Selective_ReadCharRaw (int fd) |
| 258 |
{ |
| 259 |
char ch; |
| 260 |
|
| 261 |
read (fd, &ch, 1); |
| 262 |
return ch; |
| 263 |
} |
| 264 |
|
| 265 |
EXTERN |
| 266 |
void |
| 267 |
_M2_Selective_init () |
| 268 |
{ |
| 269 |
} |
| 270 |
|
| 271 |
EXTERN |
| 272 |
void |
| 273 |
_M2_Selective_fini () |
| 274 |
{ |
| 275 |
} |