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