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