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