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