0dbc77625e41c5b34f876675f28ccda5f2d34ce5
[packages/random.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2 * $Id: HsBase.h,v 1.12 2002/08/30 14:54:58 simonpj Exp $
3 *
4 * (c) The University of Glasgow 2001-2002
5 *
6 * Definitions for package `core' which are visible in Haskell land.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #ifndef HSCORE_H
11 #define HSCORE_H
12
13 #include "config.h"
14 #include "HsFFI.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #ifdef HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif
32 #ifdef HAVE_TERMIOS_H
33 #include <termios.h>
34 #endif
35 #ifdef HAVE_SIGNAL_H
36 #include <signal.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #ifdef HAVE_DIRENT_H
45 #include <dirent.h>
46 #endif
47 #ifdef HAVE_UTIME_H
48 #include <utime.h>
49 #endif
50 #if defined(HAVE_GETTIMEOFDAY)
51 # ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 # endif
54 #elif defined(HAVE_GETCLOCK)
55 # ifdef HAVE_SYS_TIMERS_H
56 # define POSIX_4D9 1
57 # include <sys/timers.h>
58 # endif
59 #endif
60 #if defined(HAVE_TIME_H)
61 # include <time.h>
62 #endif
63 #ifdef HAVE_SYS_TIMEB_H
64 #include <sys/timeb.h>
65 #endif
66 #ifdef HAVE_WINDOWS_H
67 #include <windows.h>
68 #endif
69 #ifdef HAVE_SYS_TIMES_H
70 #include <sys/times.h>
71 #endif
72 #if defined(HAVE_WINSOCK_H) && defined(__MINGW32__)
73 #include <winsock.h>
74 #endif
75 #ifdef HAVE_LIMITS_H
76 #include <limits.h>
77 #endif
78
79 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
80 # if defined(HAVE_SYS_RESOURCE_H)
81 # include <sys/resource.h>
82 # endif
83 #endif
84
85 #ifdef hpux_TARGET_OS
86 #include <sys/syscall.h>
87 #define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
88 #define HAVE_GETRUSAGE
89 #endif
90
91 /* For System */
92 #ifdef HAVE_SYS_WAIT_H
93 #include <sys/wait.h>
94 #endif
95 #ifdef HAVE_VFORK_H
96 #include <vfork.h>
97 #endif
98 #include "lockFile.h"
99 #include "dirUtils.h"
100 #include "errUtils.h"
101
102 #if defined(__MINGW32__)
103 #include <io.h>
104 #include <fcntl.h>
105 #include "timeUtils.h"
106 #endif
107
108 /* in ghc_errno.c */
109 int *ghcErrno(void);
110
111 /* in system.c */
112 HsInt systemCmd(HsAddr cmd);
113
114 /* in rawSystem.c */
115 HsInt rawSystemCmd(HsAddr cmd);
116
117 /* in inputReady.c */
118 int inputReady(int fd, int msecs, int isSock);
119
120 /* -----------------------------------------------------------------------------
121 64-bit operations, defined in longlong.c
122 -------------------------------------------------------------------------- */
123
124 #ifdef SUPPORT_LONG_LONGS
125
126 StgInt stg_gtWord64 (StgWord64, StgWord64);
127 StgInt stg_geWord64 (StgWord64, StgWord64);
128 StgInt stg_eqWord64 (StgWord64, StgWord64);
129 StgInt stg_neWord64 (StgWord64, StgWord64);
130 StgInt stg_ltWord64 (StgWord64, StgWord64);
131 StgInt stg_leWord64 (StgWord64, StgWord64);
132
133 StgInt stg_gtInt64 (StgInt64, StgInt64);
134 StgInt stg_geInt64 (StgInt64, StgInt64);
135 StgInt stg_eqInt64 (StgInt64, StgInt64);
136 StgInt stg_neInt64 (StgInt64, StgInt64);
137 StgInt stg_ltInt64 (StgInt64, StgInt64);
138 StgInt stg_leInt64 (StgInt64, StgInt64);
139
140 StgWord64 stg_remWord64 (StgWord64, StgWord64);
141 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
142
143 StgInt64 stg_remInt64 (StgInt64, StgInt64);
144 StgInt64 stg_quotInt64 (StgInt64, StgInt64);
145 StgInt64 stg_negateInt64 (StgInt64);
146 StgInt64 stg_plusInt64 (StgInt64, StgInt64);
147 StgInt64 stg_minusInt64 (StgInt64, StgInt64);
148 StgInt64 stg_timesInt64 (StgInt64, StgInt64);
149
150 StgWord64 stg_and64 (StgWord64, StgWord64);
151 StgWord64 stg_or64 (StgWord64, StgWord64);
152 StgWord64 stg_xor64 (StgWord64, StgWord64);
153 StgWord64 stg_not64 (StgWord64);
154
155 StgWord64 stg_uncheckedShiftL64 (StgWord64, StgInt);
156 StgWord64 stg_uncheckedShiftRL64 (StgWord64, StgInt);
157 StgInt64 stg_uncheckedIShiftL64 (StgInt64, StgInt);
158 StgInt64 stg_uncheckedIShiftRL64 (StgInt64, StgInt);
159 StgInt64 stg_uncheckedIShiftRA64 (StgInt64, StgInt);
160
161 StgInt64 stg_intToInt64 (StgInt);
162 StgInt stg_int64ToInt (StgInt64);
163 StgWord64 stg_int64ToWord64 (StgInt64);
164
165 StgWord64 stg_wordToWord64 (StgWord);
166 StgWord stg_word64ToWord (StgWord64);
167 StgInt64 stg_word64ToInt64 (StgWord64);
168
169 StgInt64 stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
170 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
171
172 #endif /* SUPPORT_LONG_LONGS */
173
174 /* -----------------------------------------------------------------------------
175 INLINE functions.
176
177 These functions are given as inlines here for when compiling via C,
178 but we also generate static versions into the cbits library for
179 when compiling to native code.
180 -------------------------------------------------------------------------- */
181
182 #ifndef INLINE
183 #define INLINE extern inline
184 #endif
185
186 #if !defined(mingw32_TARGET_OS)
187 INLINE int
188 __hscore_sigaddset( sigset_t * set, int s )
189 { return sigaddset(set,s); }
190 #endif
191
192 INLINE int __hscore_s_isreg(m) { return S_ISREG(m); }
193 INLINE int __hscore_s_isdir(m) { return S_ISDIR(m); }
194 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
195 INLINE int __hscore_s_isblk(m) { return S_ISBLK(m); }
196 INLINE int __hscore_s_ischr(m) { return S_ISCHR(m); }
197 #ifdef S_ISSOCK
198 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
199 #endif
200
201 #ifndef mingw32_TARGET_OS
202 INLINE void
203 __hscore_sigemptyset( sigset_t *set )
204 { sigemptyset(set); }
205 #endif
206
207 INLINE void *
208 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
209 { return memcpy(dst+dst_off, src, sz); }
210
211 INLINE void *
212 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
213 { return memcpy(dst, src+src_off, sz); }
214
215 INLINE HsBool
216 __hscore_supportsTextMode()
217 {
218 #if defined(mingw32_TARGET_OS)
219 return HS_BOOL_FALSE;
220 #else
221 return HS_BOOL_TRUE;
222 #endif
223 }
224
225 INLINE HsInt
226 __hscore_bufsiz()
227 {
228 return BUFSIZ;
229 }
230
231 INLINE HsInt
232 __hscore_seek_cur()
233 {
234 return SEEK_CUR;
235 }
236
237 INLINE HsInt
238 __hscore_o_binary()
239 {
240 #ifdef HAVE_O_BINARY
241 return O_BINARY;
242 #else
243 return 0;
244 #endif
245 }
246
247 INLINE int
248 __hscore_o_rdonly()
249 {
250 #ifdef O_RDONLY
251 return O_RDONLY;
252 #else
253 return 0;
254 #endif
255 }
256
257 INLINE int
258 __hscore_o_wronly( void )
259 {
260 #ifdef O_WRONLY
261 return O_WRONLY;
262 #else
263 return 0;
264 #endif
265 }
266
267 INLINE int
268 __hscore_o_rdwr( void )
269 {
270 #ifdef O_RDWR
271 return O_RDWR;
272 #else
273 return 0;
274 #endif
275 }
276
277 INLINE int
278 __hscore_o_append( void )
279 {
280 #ifdef O_APPEND
281 return O_APPEND;
282 #else
283 return 0;
284 #endif
285 }
286
287 INLINE int
288 __hscore_o_creat( void )
289 {
290 #ifdef O_CREAT
291 return O_CREAT;
292 #else
293 return 0;
294 #endif
295 }
296
297 INLINE int
298 __hscore_o_excl( void )
299 {
300 #ifdef O_EXCL
301 return O_EXCL;
302 #else
303 return 0;
304 #endif
305 }
306
307 INLINE int
308 __hscore_o_trunc( void )
309 {
310 #ifdef O_TRUNC
311 return O_TRUNC;
312 #else
313 return 0;
314 #endif
315 }
316
317 INLINE int
318 __hscore_o_noctty( void )
319 {
320 #ifdef O_NOCTTY
321 return O_NOCTTY;
322 #else
323 return 0;
324 #endif
325 }
326
327 INLINE int
328 __hscore_o_nonblock( void )
329 {
330 #ifdef O_NONBLOCK
331 return O_NONBLOCK;
332 #else
333 return 0;
334 #endif
335 }
336
337 INLINE HsInt
338 __hscore_seek_set( void )
339 {
340 return SEEK_SET;
341 }
342
343 INLINE HsInt
344 __hscore_seek_end( void )
345 {
346 return SEEK_END;
347 }
348
349 INLINE HsInt
350 __hscore_setmode( HsInt fd, HsBool toBin )
351 {
352 #if defined(__MINGW32__)
353 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
354 #else
355 return 0;
356 #endif
357 }
358
359 INLINE HsInt
360 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr,
361 HsInt off, int sz )
362 {
363 #if defined(__MINGW32__)
364 if (isSock) {
365 return send(fd,ptr + off, sz, 0);
366 }
367 #endif
368 return write(fd,ptr + off, sz);
369 }
370
371 INLINE HsInt
372 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr,
373 HsInt off, int sz )
374 {
375 #if defined(__MINGW32__)
376 if (isSock) {
377 return recv(fd,ptr + off, sz, 0);
378 }
379 #endif
380 return read(fd,ptr + off, sz);
381
382 }
383
384 #if defined(__MINGW32__)
385 INLINE long *
386 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
387
388 INLINE char **
389 __hscore_Time_ghcTzname( void ) { return _tzname; }
390 #endif
391
392 INLINE HsInt
393 __hscore_mkdir( HsAddr pathName, HsInt mode )
394 {
395 #if defined(__MINGW32__)
396 return mkdir(pathName);
397 #else
398 return mkdir(pathName,mode);
399 #endif
400 }
401
402 INLINE HsInt
403 __hscore_lstat( HsAddr fname, HsAddr st )
404 {
405 #ifdef HAVE_LSTAT
406 return lstat((const char*)fname, (struct stat*)st);
407 #else
408 return stat((const char*)fname, (struct stat*)st);
409 #endif
410 }
411
412 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
413
414 INLINE mode_t __hscore_R_OK() { return R_OK; }
415 INLINE mode_t __hscore_W_OK() { return W_OK; }
416 INLINE mode_t __hscore_X_OK() { return X_OK; }
417
418 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
419 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
420 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
421
422 INLINE HsAddr
423 __hscore_d_name( struct dirent* d )
424 {
425 #ifndef mingw32_TARGET_OS
426 return (HsAddr)(&d->d_name);
427 #else
428 return (HsAddr)(d->d_name);
429 #endif
430 }
431
432 INLINE HsInt
433 __hscore_end_of_dir( void )
434 {
435 #ifndef mingw32_TARGET_OS
436 return 0;
437 #else
438 return ENOENT;
439 #endif
440 }
441
442 INLINE void
443 __hscore_free_dirent(HsAddr dEnt)
444 {
445 #if HAVE_READDIR_R
446 free(dEnt);
447 #endif
448 }
449
450 INLINE HsInt
451 __hscore_sizeof_stat( void )
452 {
453 return sizeof(struct stat);
454 }
455
456 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
457 INLINE off_t __hscore_st_size ( struct stat* st ) { return st->st_size; }
458 INLINE mode_t __hscore_st_mode ( struct stat* st ) { return st->st_mode; }
459
460 #if HAVE_TERMIOS_H
461 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
462
463 INLINE void
464 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
465
466 INLINE unsigned char*
467 __hscore_ptr_c_cc( struct termios* ts )
468 { return (unsigned char*) &ts->c_cc; }
469 #endif
470
471 INLINE HsInt
472 __hscore_sizeof_termios( void )
473 {
474 #ifndef mingw32_TARGET_OS
475 return sizeof(struct termios);
476 #else
477 return 0;
478 #endif
479 }
480
481 INLINE HsInt
482 __hscore_sizeof_sigset_t( void )
483 {
484 #ifndef mingw32_TARGET_OS
485 return sizeof(sigset_t);
486 #else
487 return 0;
488 #endif
489 }
490
491 INLINE int
492 __hscore_echo( void )
493 {
494 #ifdef ECHO
495 return ECHO;
496 #else
497 return 0;
498 #endif
499
500 }
501
502 INLINE int
503 __hscore_tcsanow( void )
504 {
505 #ifdef TCSANOW
506 return TCSANOW;
507 #else
508 return 0;
509 #endif
510
511 }
512
513 INLINE int
514 __hscore_icanon( void )
515 {
516 #ifdef ICANON
517 return ICANON;
518 #else
519 return 0;
520 #endif
521 }
522
523 INLINE int __hscore_vmin( void )
524 {
525 #ifdef VMIN
526 return VMIN;
527 #else
528 return 0;
529 #endif
530 }
531
532 INLINE int __hscore_vtime( void )
533 {
534 #ifdef VTIME
535 return VTIME;
536 #else
537 return 0;
538 #endif
539 }
540
541 INLINE int __hscore_sigttou( void )
542 {
543 #ifdef SIGTTOU
544 return SIGTTOU;
545 #else
546 return 0;
547 #endif
548 }
549
550 INLINE int __hscore_sig_block( void )
551 {
552 #ifdef SIG_BLOCK
553 return SIG_BLOCK;
554 #else
555 return 0;
556 #endif
557 }
558
559 INLINE int __hscore_sig_setmask( void )
560 {
561 #ifdef SIG_SETMASK
562 return SIG_SETMASK;
563 #else
564 return 0;
565 #endif
566 }
567
568 INLINE int
569 __hscore_f_getfl( void )
570 {
571 #ifdef F_GETFL
572 return F_GETFL;
573 #else
574 return 0;
575 #endif
576 }
577
578 INLINE int
579 __hscore_f_setfl( void )
580 {
581 #ifdef F_SETFL
582 return F_SETFL;
583 #else
584 return 0;
585 #endif
586 }
587
588 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
589
590 #endif
591