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