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