[project @ 2003-11-02 16:30:16 by panne]
[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 "config.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 #endif
116
117 /* in ghc_errno.c */
118 int *ghcErrno(void);
119
120 /* in system.c */
121 HsInt systemCmd(HsAddr cmd);
122
123 /* in rawSystem.c */
124 #if defined(mingw32_TARGET_OS)
125 HsInt rawSystem(HsAddr cmd);
126 #else
127 HsInt rawSystem(HsAddr cmd, HsAddr args);
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_hdr, HsAddr msg, HsInt len);
135
136 /* in Signals.c */
137 extern HsInt nocldstop;
138
139 /* -----------------------------------------------------------------------------
140    64-bit operations, defined in longlong.c
141    -------------------------------------------------------------------------- */
142
143 #ifdef SUPPORT_LONG_LONGS
144
145 StgInt stg_gtWord64 (StgWord64, StgWord64);
146 StgInt stg_geWord64 (StgWord64, StgWord64);
147 StgInt stg_eqWord64 (StgWord64, StgWord64);
148 StgInt stg_neWord64 (StgWord64, StgWord64);
149 StgInt stg_ltWord64 (StgWord64, StgWord64);
150 StgInt stg_leWord64 (StgWord64, StgWord64);
151
152 StgInt stg_gtInt64 (StgInt64, StgInt64);
153 StgInt stg_geInt64 (StgInt64, StgInt64);
154 StgInt stg_eqInt64 (StgInt64, StgInt64);
155 StgInt stg_neInt64 (StgInt64, StgInt64);
156 StgInt stg_ltInt64 (StgInt64, StgInt64);
157 StgInt stg_leInt64 (StgInt64, StgInt64);
158
159 StgWord64 stg_remWord64  (StgWord64, StgWord64);
160 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
161
162 StgInt64 stg_remInt64    (StgInt64, StgInt64);
163 StgInt64 stg_quotInt64   (StgInt64, StgInt64);
164 StgInt64 stg_negateInt64 (StgInt64);
165 StgInt64 stg_plusInt64   (StgInt64, StgInt64);
166 StgInt64 stg_minusInt64  (StgInt64, StgInt64);
167 StgInt64 stg_timesInt64  (StgInt64, StgInt64);
168
169 StgWord64 stg_and64  (StgWord64, StgWord64);
170 StgWord64 stg_or64   (StgWord64, StgWord64);
171 StgWord64 stg_xor64  (StgWord64, StgWord64);
172 StgWord64 stg_not64  (StgWord64);
173
174 StgWord64 stg_uncheckedShiftL64   (StgWord64, StgInt);
175 StgWord64 stg_uncheckedShiftRL64  (StgWord64, StgInt);
176 StgInt64  stg_uncheckedIShiftL64  (StgInt64, StgInt);
177 StgInt64  stg_uncheckedIShiftRL64 (StgInt64, StgInt);
178 StgInt64  stg_uncheckedIShiftRA64 (StgInt64, StgInt);
179
180 StgInt64  stg_intToInt64    (StgInt);
181 StgInt    stg_int64ToInt    (StgInt64);
182 StgWord64 stg_int64ToWord64 (StgInt64);
183
184 StgWord64 stg_wordToWord64  (StgWord);
185 StgWord   stg_word64ToWord  (StgWord64);
186 StgInt64  stg_word64ToInt64 (StgWord64);
187
188 StgInt64  stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
189 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
190
191 #endif /* SUPPORT_LONG_LONGS */
192
193 /* -----------------------------------------------------------------------------
194    INLINE functions.
195
196    These functions are given as inlines here for when compiling via C,
197    but we also generate static versions into the cbits library for
198    when compiling to native code.
199    -------------------------------------------------------------------------- */
200
201 #ifndef INLINE
202 # if defined(_MSC_VER)
203 #  define INLINE extern __inline
204 # else
205 #  define INLINE extern inline
206 # endif
207 #endif
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, HsBool isSock, HsAddr ptr, 
396                            HsInt off, int sz )
397 {
398 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
399   if (isSock) {
400     return send(fd,(char *)ptr + off, sz, 0);
401   }
402 #endif
403   return write(fd,(char *)ptr + off, sz);
404 }
405
406 INLINE HsInt
407 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
408                           HsInt off, int sz )
409 {
410 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
411   if (isSock) {
412     return recv(fd,(char *)ptr + off, sz, 0);
413   }
414 #endif
415   return read(fd,(char *)ptr + off, sz);
416
417 }
418
419 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
420 INLINE long *
421 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
422
423 INLINE char **
424 __hscore_Time_ghcTzname( void ) { return _tzname; }
425 #endif
426
427 INLINE HsInt
428 __hscore_mkdir( HsAddr pathName, HsInt mode )
429 {
430 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
431   return mkdir(pathName);
432 #else
433   return mkdir(pathName,mode);
434 #endif
435 }
436
437 INLINE HsInt
438 __hscore_lstat( HsAddr fname, HsAddr st )
439 {
440 #if HAVE_LSTAT
441   return lstat((const char*)fname, (struct stat*)st);
442 #else
443   return stat((const char*)fname, (struct stat*)st);
444 #endif
445 }
446
447 #ifdef PATH_MAX
448 /* A size that will contain many path names, but not necessarily all
449  * (PATH_MAX is not defined on systems with unlimited path length,
450  * e.g. the Hurd).
451  */
452 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
453 #else
454 INLINE HsInt __hscore_long_path_size() { return 4096; }
455 #endif
456
457 #ifdef R_OK
458 INLINE mode_t __hscore_R_OK() { return R_OK; }
459 #endif
460 #ifdef W_OK
461 INLINE mode_t __hscore_W_OK() { return W_OK; }
462 #endif
463 #ifdef X_OK
464 INLINE mode_t __hscore_X_OK() { return X_OK; }
465 #endif
466
467 #ifdef S_IRUSR
468 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
469 #endif
470 #ifdef S_IWUSR
471 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
472 #endif
473 #ifdef S_IXUSR
474 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
475 #endif
476
477 #if !defined(_MSC_VER)
478 INLINE HsAddr
479 __hscore_d_name( struct dirent* d )
480
481 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
482   return (HsAddr)(&d->d_name);
483 #else
484   return (HsAddr)(d->d_name);
485 #endif
486 }
487 #endif
488
489 INLINE HsInt
490 __hscore_end_of_dir( void )
491 {
492 #ifndef mingw32_TARGET_OS
493   return 0;
494 #else
495   return ENOENT;
496 #endif  
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 #if HAVE_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 #endif /* mingw32_TARGET_OS */
687
688 #endif /* __HSBASE_H__ */
689