portable implementation of WordPtr/IntPtr for non-GHC
[ghc-base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The University of Glasgow 2001-2004
4  *
5  * Definitions for package `base' which are visible in Haskell land.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef __HSBASE_H__
10 #define __HSBASE_H__
11
12 #include "ghcconfig.h"
13
14 #include "HsBaseConfig.h"
15
16 /* ultra-evil... */
17 #undef PACKAGE_BUGREPORT
18 #undef PACKAGE_NAME
19 #undef PACKAGE_STRING
20 #undef PACKAGE_TARNAME
21 #undef PACKAGE_VERSION
22
23 #include "HsFFI.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 #if HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #if HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 #if HAVE_FCNTL_H
39 # include <fcntl.h>
40 #endif
41 #if HAVE_TERMIOS_H
42 #include <termios.h>
43 #endif
44 #if HAVE_SIGNAL_H
45 #include <signal.h>
46 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
47 #if __OpenBSD__
48 #undef sigemptyset
49 #undef sigfillset
50 #endif
51 #endif
52 #if HAVE_ERRNO_H
53 #include <errno.h>
54 #endif
55 #if HAVE_STRING_H
56 #include <string.h>
57 #endif
58 #if HAVE_DIRENT_H
59 #include <dirent.h>
60 #endif
61 #if HAVE_UTIME_H
62 #include <utime.h>
63 #endif
64 #if HAVE_SYS_UTSNAME_H
65 #include <sys/utsname.h>
66 #endif
67 #if HAVE_GETTIMEOFDAY
68 #  if HAVE_SYS_TIME_H
69 #   include <sys/time.h>
70 #  endif
71 #elif HAVE_GETCLOCK
72 # if HAVE_SYS_TIMERS_H
73 #  define POSIX_4D9 1
74 #  include <sys/timers.h>
75 # endif
76 #endif
77 #if HAVE_TIME_H
78 #include <time.h>
79 #endif
80 #if HAVE_SYS_TIMEB_H
81 #include <sys/timeb.h>
82 #endif
83 #if HAVE_WINDOWS_H
84 #include <windows.h>
85 #endif
86 #if HAVE_SYS_TIMES_H
87 #include <sys/times.h>
88 #endif
89 #if HAVE_WINSOCK_H && defined(mingw32_HOST_OS)
90 #include <winsock.h>
91 #endif
92 #if HAVE_LIMITS_H
93 #include <limits.h>
94 #endif
95 #if HAVE_WCTYPE_H
96 #include <wctype.h>
97 #endif
98 #if HAVE_INTTYPES_H
99 # include <inttypes.h>
100 #elif HAVE_STDINT_H
101 # include <stdint.h>
102 #endif
103
104 #if !defined(mingw32_HOST_OS) && !defined(irix_HOST_OS)
105 # if HAVE_SYS_RESOURCE_H
106 #  include <sys/resource.h>
107 # endif
108 #endif
109
110 #ifdef hpux_HOST_OS
111 #include <sys/syscall.h>
112 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
113 #define HAVE_GETRUSAGE
114 #endif
115
116 /* For System */
117 #if HAVE_SYS_WAIT_H
118 #include <sys/wait.h>
119 #endif
120 #if HAVE_VFORK_H
121 #include <vfork.h>
122 #endif
123 #include "lockFile.h"
124 #include "dirUtils.h"
125 #include "WCsubst.h"
126
127 #include "runProcess.h"
128
129 #if defined(mingw32_HOST_OS)
130 #include <io.h>
131 #include <fcntl.h>
132 #include "timeUtils.h"
133 #include <shlobj.h>
134 #include <share.h>
135 #endif
136
137 #if HAVE_SYS_SELECT_H
138 #include <sys/select.h>
139 #endif
140
141 /* in inputReady.c */
142 int inputReady(int fd, int msecs, int isSock);
143
144 /* in Signals.c */
145 extern HsInt nocldstop;
146
147 #if !defined(mingw32_HOST_OS)
148 /* in execvpe.c */
149 extern int execvpe(char *name, char *const argv[], char **envp);
150 extern void pPrPr_disableITimers (void);
151 #endif
152
153 /* -----------------------------------------------------------------------------
154    64-bit operations, defined in longlong.c
155    -------------------------------------------------------------------------- */
156
157 #ifdef SUPPORT_LONG_LONGS
158
159 StgInt stg_gtWord64 (StgWord64, StgWord64);
160 StgInt stg_geWord64 (StgWord64, StgWord64);
161 StgInt stg_eqWord64 (StgWord64, StgWord64);
162 StgInt stg_neWord64 (StgWord64, StgWord64);
163 StgInt stg_ltWord64 (StgWord64, StgWord64);
164 StgInt stg_leWord64 (StgWord64, StgWord64);
165
166 StgInt stg_gtInt64 (StgInt64, StgInt64);
167 StgInt stg_geInt64 (StgInt64, StgInt64);
168 StgInt stg_eqInt64 (StgInt64, StgInt64);
169 StgInt stg_neInt64 (StgInt64, StgInt64);
170 StgInt stg_ltInt64 (StgInt64, StgInt64);
171 StgInt stg_leInt64 (StgInt64, StgInt64);
172
173 StgWord64 stg_remWord64  (StgWord64, StgWord64);
174 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
175
176 StgInt64 stg_remInt64    (StgInt64, StgInt64);
177 StgInt64 stg_quotInt64   (StgInt64, StgInt64);
178 StgInt64 stg_negateInt64 (StgInt64);
179 StgInt64 stg_plusInt64   (StgInt64, StgInt64);
180 StgInt64 stg_minusInt64  (StgInt64, StgInt64);
181 StgInt64 stg_timesInt64  (StgInt64, StgInt64);
182
183 StgWord64 stg_and64  (StgWord64, StgWord64);
184 StgWord64 stg_or64   (StgWord64, StgWord64);
185 StgWord64 stg_xor64  (StgWord64, StgWord64);
186 StgWord64 stg_not64  (StgWord64);
187
188 StgWord64 stg_uncheckedShiftL64   (StgWord64, StgInt);
189 StgWord64 stg_uncheckedShiftRL64  (StgWord64, StgInt);
190 StgInt64  stg_uncheckedIShiftL64  (StgInt64, StgInt);
191 StgInt64  stg_uncheckedIShiftRL64 (StgInt64, StgInt);
192 StgInt64  stg_uncheckedIShiftRA64 (StgInt64, StgInt);
193
194 StgInt64  stg_intToInt64    (StgInt);
195 StgInt    stg_int64ToInt    (StgInt64);
196 StgWord64 stg_int64ToWord64 (StgInt64);
197
198 StgWord64 stg_wordToWord64  (StgWord);
199 StgWord   stg_word64ToWord  (StgWord64);
200 StgInt64  stg_word64ToInt64 (StgWord64);
201
202 StgInt64  stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
203 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
204
205 #endif /* SUPPORT_LONG_LONGS */
206
207 /* -----------------------------------------------------------------------------
208    INLINE functions.
209
210    These functions are given as inlines here for when compiling via C,
211    but we also generate static versions into the cbits library for
212    when compiling to native code.
213    -------------------------------------------------------------------------- */
214
215 #ifndef INLINE
216 # if defined(_MSC_VER)
217 #  define INLINE extern __inline
218 # elif defined(__GNUC__)
219 #  define INLINE extern inline
220 # else
221 #  define INLINE inline
222 # endif
223 #endif
224
225 INLINE int __hscore_get_errno(void) { return errno; }
226 INLINE void __hscore_set_errno(int e) { errno = e; }
227
228 #if !defined(_MSC_VER)
229 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
230 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
231 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
232 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
233 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
234 #ifdef S_ISSOCK
235 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
236 #endif
237 #endif
238
239 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
240 INLINE int
241 __hscore_sigemptyset( sigset_t *set )
242 { return sigemptyset(set); }
243
244 INLINE int
245 __hscore_sigfillset( sigset_t *set )
246 { return sigfillset(set); }
247
248 INLINE int
249 __hscore_sigaddset( sigset_t * set, int s )
250 { return sigaddset(set,s); }
251
252 INLINE int
253 __hscore_sigdelset( sigset_t * set, int s )
254 { return sigdelset(set,s); }
255
256 INLINE int
257 __hscore_sigismember( sigset_t * set, int s )
258 { return sigismember(set,s); }
259 #endif
260
261 INLINE void *
262 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
263 { return memcpy(dst+dst_off, src, sz); }
264
265 INLINE void *
266 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
267 { return memcpy(dst, src+src_off, sz); }
268
269 INLINE HsBool
270 __hscore_supportsTextMode()
271 {
272 #if defined(mingw32_HOST_OS)
273   return HS_BOOL_FALSE;
274 #else
275   return HS_BOOL_TRUE;
276 #endif
277 }
278
279 INLINE HsInt
280 __hscore_bufsiz()
281 {
282   return BUFSIZ;
283 }
284
285 INLINE HsInt
286 __hscore_seek_cur()
287 {
288   return SEEK_CUR;
289 }
290
291 INLINE HsInt
292 __hscore_o_binary()
293 {
294 #if defined(_MSC_VER)
295   return O_BINARY;
296 #else
297   return CONST_O_BINARY;
298 #endif
299 }
300
301 INLINE int
302 __hscore_o_rdonly()
303 {
304 #ifdef O_RDONLY
305   return O_RDONLY;
306 #else
307   return 0;
308 #endif
309 }
310
311 INLINE int
312 __hscore_o_wronly( void )
313 {
314 #ifdef O_WRONLY
315   return O_WRONLY;
316 #else
317   return 0;
318 #endif
319 }
320
321 INLINE int
322 __hscore_o_rdwr( void )
323 {
324 #ifdef O_RDWR
325   return O_RDWR;
326 #else
327   return 0;
328 #endif
329 }
330
331 INLINE int
332 __hscore_o_append( void )
333 {
334 #ifdef O_APPEND
335   return O_APPEND;
336 #else
337   return 0;
338 #endif
339 }
340
341 INLINE int
342 __hscore_o_creat( void )
343 {
344 #ifdef O_CREAT
345   return O_CREAT;
346 #else
347   return 0;
348 #endif
349 }
350
351 INLINE int
352 __hscore_o_excl( void )
353 {
354 #ifdef O_EXCL
355   return O_EXCL;
356 #else
357   return 0;
358 #endif
359 }
360
361 INLINE int
362 __hscore_o_trunc( void )
363 {
364 #ifdef O_TRUNC
365   return O_TRUNC;
366 #else
367   return 0;
368 #endif
369 }
370
371 INLINE int
372 __hscore_o_noctty( void )
373 {
374 #ifdef O_NOCTTY
375   return O_NOCTTY;
376 #else
377   return 0;
378 #endif
379 }
380
381 INLINE int
382 __hscore_o_nonblock( void )
383 {
384 #ifdef O_NONBLOCK
385   return O_NONBLOCK;
386 #else
387   return 0;
388 #endif
389 }
390
391 INLINE HsInt
392 __hscore_seek_set( void )
393 {
394   return SEEK_SET;
395 }
396
397 INLINE HsInt
398 __hscore_seek_end( void )
399 {
400   return SEEK_END;
401 }
402
403 INLINE int
404 __hscore_ftruncate( int fd, off_t where )
405 {
406 #if defined(HAVE_FTRUNCATE)
407   return ftruncate(fd,where);
408 #elif defined(HAVE__CHSIZE)
409   return _chsize(fd,where);
410 #else
411 #error at least ftruncate or _chsize functions are required to build
412 #endif
413 }
414
415 INLINE HsInt
416 __hscore_setmode( HsInt fd, HsBool toBin )
417 {
418 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
419   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
420 #else
421   return 0;
422 #endif
423 }
424
425 #if __GLASGOW_HASKELL__
426
427 INLINE HsInt
428 __hscore_PrelHandle_write( HsInt fd, HsAddr ptr, HsInt off, int sz )
429 {
430   return write(fd,(char *)ptr + off, sz);
431 }
432
433 INLINE HsInt
434 __hscore_PrelHandle_read( HsInt fd, HsAddr ptr, HsInt off, int sz )
435 {
436   return read(fd,(char *)ptr + off, sz);
437
438 }
439
440 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
441 INLINE HsInt
442 __hscore_PrelHandle_send( HsInt fd, HsAddr ptr, HsInt off, int sz )
443 {
444     return send(fd,(char *)ptr + off, sz, 0);
445 }
446
447 INLINE HsInt
448 __hscore_PrelHandle_recv( HsInt fd, HsAddr ptr, HsInt off, int sz )
449 {
450     return recv(fd,(char *)ptr + off, sz, 0);
451 }
452 #endif
453
454 #endif /* __GLASGOW_HASKELL__ */
455
456 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
457 INLINE long *
458 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
459
460 INLINE char **
461 __hscore_Time_ghcTzname( void ) { return _tzname; }
462 #endif
463
464 INLINE HsInt
465 __hscore_mkdir( HsAddr pathName, HsInt mode )
466 {
467 #if defined(mingw32_HOST_OS) || defined(_MSC_VER)
468   return mkdir(pathName);
469 #else
470   return mkdir(pathName,mode);
471 #endif
472 }
473
474 INLINE HsInt
475 __hscore_lstat( HsAddr fname, HsAddr st )
476 {
477 #if HAVE_LSTAT
478   return lstat((const char*)fname, (struct stat*)st);
479 #else
480   return stat((const char*)fname, (struct stat*)st);
481 #endif
482 }
483
484 #ifdef PATH_MAX
485 /* A size that will contain many path names, but not necessarily all
486  * (PATH_MAX is not defined on systems with unlimited path length,
487  * e.g. the Hurd).
488  */
489 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
490 #else
491 INLINE HsInt __hscore_long_path_size() { return 4096; }
492 #endif
493
494 #ifdef R_OK
495 INLINE mode_t __hscore_R_OK() { return R_OK; }
496 #endif
497 #ifdef W_OK
498 INLINE mode_t __hscore_W_OK() { return W_OK; }
499 #endif
500 #ifdef X_OK
501 INLINE mode_t __hscore_X_OK() { return X_OK; }
502 #endif
503
504 #ifdef S_IRUSR
505 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
506 #endif
507 #ifdef S_IWUSR
508 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
509 #endif
510 #ifdef S_IXUSR
511 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
512 #endif
513
514 INLINE HsAddr
515 __hscore_d_name( struct dirent* d )
516 {
517   return (HsAddr)(d->d_name);
518 }
519
520 INLINE HsInt
521 __hscore_end_of_dir( void )
522 {
523   return READDIR_ERRNO_EOF;
524 }
525
526 INLINE void
527 __hscore_free_dirent(HsAddr dEnt)
528 {
529 #if HAVE_READDIR_R
530   free(dEnt);
531 #endif
532 }
533
534 INLINE HsInt
535 __hscore_sizeof_stat( void )
536 {
537   return sizeof(struct stat);
538 }
539
540 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
541 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
542 #if !defined(_MSC_VER)
543 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
544 #endif
545
546 #if HAVE_TERMIOS_H
547 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
548
549 INLINE void
550 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
551
552 INLINE unsigned char*
553 __hscore_ptr_c_cc( struct termios* ts )
554 { return (unsigned char*) &ts->c_cc; }
555
556 INLINE HsInt
557 __hscore_sizeof_termios( void )
558 {
559 #ifndef mingw32_HOST_OS
560   return sizeof(struct termios);
561 #else
562   return 0;
563 #endif
564 }
565 #endif
566
567 #if !defined(mingw32_HOST_OS) && !defined(_MSC_VER)
568 INLINE HsInt
569 __hscore_sizeof_sigset_t( void )
570 {
571   return sizeof(sigset_t);
572 }
573 #endif
574
575 INLINE int
576 __hscore_echo( void )
577 {
578 #ifdef ECHO
579   return ECHO;
580 #else
581   return 0;
582 #endif
583
584 }
585
586 INLINE int
587 __hscore_tcsanow( void )
588 {
589 #ifdef TCSANOW
590   return TCSANOW;
591 #else
592   return 0;
593 #endif
594
595 }
596
597 INLINE int
598 __hscore_icanon( void )
599 {
600 #ifdef ICANON
601   return ICANON;
602 #else
603   return 0;
604 #endif
605 }
606
607 INLINE int __hscore_vmin( void )
608 {
609 #ifdef VMIN
610   return VMIN;
611 #else
612   return 0;
613 #endif
614 }
615
616 INLINE int __hscore_vtime( void )
617 {
618 #ifdef VTIME
619   return VTIME;
620 #else
621   return 0;
622 #endif
623 }
624
625 INLINE int __hscore_sigttou( void )
626 {
627 #ifdef SIGTTOU
628   return SIGTTOU;
629 #else
630   return 0;
631 #endif
632 }
633
634 INLINE int __hscore_sig_block( void )
635 {
636 #ifdef SIG_BLOCK
637   return SIG_BLOCK;
638 #else
639   return 0;
640 #endif
641 }
642
643 INLINE int __hscore_sig_setmask( void )
644 {
645 #ifdef SIG_SETMASK
646   return SIG_SETMASK;
647 #else
648   return 0;
649 #endif
650 }
651
652 INLINE int
653 __hscore_f_getfl( void )
654 {
655 #ifdef F_GETFL
656   return F_GETFL;
657 #else
658   return 0;
659 #endif
660 }
661
662 INLINE int
663 __hscore_f_setfl( void )
664 {
665 #ifdef F_SETFL
666   return F_SETFL;
667 #else
668   return 0;
669 #endif
670 }
671
672 // defined in rts/RtsStartup.c.
673 extern void* __hscore_get_saved_termios(int fd);
674 extern void __hscore_set_saved_termios(int fd, void* ts);
675
676 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
677
678 INLINE int __hscore_open(char *file, int how, mode_t mode) {
679 #ifdef mingw32_HOST_OS
680         if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
681           return _sopen(file,how,_SH_DENYRW,mode);
682         else
683           return _sopen(file,how,_SH_DENYWR,mode);
684 #else
685         return open(file,how,mode);
686 #endif
687 }
688
689 // These are wrapped because on some OSs (eg. Linux) they are
690 // macros which redirect to the 64-bit-off_t versions when large file
691 // support is enabled.
692 //
693 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
694         return (lseek(fd,off,whence));
695 }
696
697 INLINE int __hscore_stat(char *file, struct stat *buf) {
698         return (stat(file,buf));
699 }
700
701 INLINE int __hscore_fstat(int fd, struct stat *buf) {
702         return (fstat(fd,buf));
703 }
704
705 // select-related stuff
706
707 #if !defined(mingw32_HOST_OS)
708 INLINE int  hsFD_SETSIZE(void) { return FD_SETSIZE; }
709 INLINE void hsFD_CLR(int fd, fd_set *fds) { FD_CLR(fd, fds); }
710 INLINE int  hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
711 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
712 INLINE int  sizeof_fd_set(void) { return sizeof(fd_set); }
713 extern void hsFD_ZERO(fd_set *fds);
714 #endif
715
716 // gettimeofday()-related
717
718 #if !defined(mingw32_HOST_OS)
719 #define TICK_FREQ  50
720
721 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
722
723 INLINE HsInt getTicksOfDay(void)
724 {
725     struct timeval tv;
726     gettimeofday(&tv, (struct timezone *) NULL);
727     return (tv.tv_sec * TICK_FREQ +
728             tv.tv_usec * TICK_FREQ / 1000000);
729 }
730
731 INLINE void setTimevalTicks(struct timeval *p, HsInt ticks)
732 {
733     p->tv_sec  = ticks / TICK_FREQ;
734     p->tv_usec = (ticks % TICK_FREQ) * (1000000 / TICK_FREQ);
735 }
736 #endif /* !defined(mingw32_HOST_OS) */
737
738 // Directory-related
739
740 #if defined(mingw32_HOST_OS)
741
742 /* Make sure we've got the reqd CSIDL_ constants in scope;
743  * w32api header files are lagging a bit in defining the full set.
744  */
745 #if !defined(CSIDL_APPDATA)
746 #define CSIDL_APPDATA 0x001a
747 #endif
748 #if !defined(CSIDL_PERSONAL)
749 #define CSIDL_PERSONAL 0x0005
750 #endif
751 #if !defined(CSIDL_PROFILE)
752 #define CSIDL_PROFILE 0x0028
753 #endif
754 #if !defined(CSIDL_WINDOWS)
755 #define CSIDL_WINDOWS 0x0024
756 #endif
757
758 INLINE int __hscore_CSIDL_PROFILE()  { return CSIDL_PROFILE;  }
759 INLINE int __hscore_CSIDL_APPDATA()  { return CSIDL_APPDATA;  }
760 INLINE int __hscore_CSIDL_WINDOWS()  { return CSIDL_WINDOWS;  }
761 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
762 #endif
763
764 #if defined(mingw32_HOST_OS)
765 INLINE unsigned int __hscore_get_osver(void) { return _osver; }
766 #endif
767
768 /* ToDo: write a feature test that doesn't assume 'environ' to
769  *    be in scope at link-time. */
770 extern char** environ;
771 INLINE char **__hscore_environ() { return environ; }
772
773 /* lossless conversions between pointers and integral types */
774 INLINE void *    __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
775 INLINE void *    __hscore_from_intptr (intptr_t n)  { return (void *)n; }
776 INLINE uintptr_t __hscore_to_uintptr  (void *p)     { return (uintptr_t)p; }
777 INLINE intptr_t  __hscore_to_intptr   (void *p)     { return (intptr_t)p; }
778
779 #endif /* __HSBASE_H__ */
780