[project @ 2003-12-23 13:58:17 by simonmar]
[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, 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 long *
409 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
410
411 INLINE char **
412 __hscore_Time_ghcTzname( void ) { return _tzname; }
413 #endif
414
415 INLINE HsInt
416 __hscore_mkdir( HsAddr pathName, HsInt mode )
417 {
418 #if defined(mingw32_TARGET_OS) || defined(_MSC_VER)
419   return mkdir(pathName);
420 #else
421   return mkdir(pathName,mode);
422 #endif
423 }
424
425 INLINE HsInt
426 __hscore_lstat( HsAddr fname, HsAddr st )
427 {
428 #if HAVE_LSTAT
429   return lstat((const char*)fname, (struct stat*)st);
430 #else
431   return stat((const char*)fname, (struct stat*)st);
432 #endif
433 }
434
435 #ifdef PATH_MAX
436 /* A size that will contain many path names, but not necessarily all
437  * (PATH_MAX is not defined on systems with unlimited path length,
438  * e.g. the Hurd).
439  */
440 INLINE HsInt __hscore_long_path_size() { return PATH_MAX; }
441 #else
442 INLINE HsInt __hscore_long_path_size() { return 4096; }
443 #endif
444
445 #ifdef R_OK
446 INLINE mode_t __hscore_R_OK() { return R_OK; }
447 #endif
448 #ifdef W_OK
449 INLINE mode_t __hscore_W_OK() { return W_OK; }
450 #endif
451 #ifdef X_OK
452 INLINE mode_t __hscore_X_OK() { return X_OK; }
453 #endif
454
455 #ifdef S_IRUSR
456 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
457 #endif
458 #ifdef S_IWUSR
459 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
460 #endif
461 #ifdef S_IXUSR
462 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
463 #endif
464
465 #if !defined(_MSC_VER)
466 INLINE HsAddr
467 __hscore_d_name( struct dirent* d )
468
469 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
470   return (HsAddr)(&d->d_name);
471 #else
472   return (HsAddr)(d->d_name);
473 #endif
474 }
475 #endif
476
477 INLINE HsInt
478 __hscore_end_of_dir( void )
479 {
480 #ifndef mingw32_TARGET_OS
481   return 0;
482 #else
483   return ENOENT;
484 #endif  
485 }
486
487 INLINE void
488 __hscore_free_dirent(HsAddr dEnt)
489 {
490 #if HAVE_READDIR_R
491   free(dEnt);
492 #endif
493 }
494
495 INLINE HsInt
496 __hscore_sizeof_stat( void )
497 {
498   return sizeof(struct stat);
499 }
500
501 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
502 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
503 #if !defined(_MSC_VER)
504 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
505 #endif
506
507 #if HAVE_TERMIOS_H
508 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
509
510 INLINE void
511 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
512
513 INLINE unsigned char*
514 __hscore_ptr_c_cc( struct termios* ts ) 
515 { return (unsigned char*) &ts->c_cc; }
516
517 INLINE HsInt
518 __hscore_sizeof_termios( void )
519 {
520 #ifndef mingw32_TARGET_OS
521   return sizeof(struct termios);
522 #else
523   return 0;
524 #endif
525 }
526 #endif
527
528 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
529 INLINE HsInt
530 __hscore_sizeof_sigset_t( void )
531 {
532   return sizeof(sigset_t);
533 }
534 #endif
535
536 INLINE int
537 __hscore_echo( void )
538 {
539 #ifdef ECHO
540   return ECHO;
541 #else
542   return 0;
543 #endif
544
545 }
546
547 INLINE int
548 __hscore_tcsanow( void )
549 {
550 #ifdef TCSANOW
551   return TCSANOW;
552 #else
553   return 0;
554 #endif
555
556 }
557
558 INLINE int
559 __hscore_icanon( void )
560 {
561 #ifdef ICANON
562   return ICANON;
563 #else
564   return 0;
565 #endif
566 }
567
568 INLINE int __hscore_vmin( void )
569 {
570 #ifdef VMIN
571   return VMIN;
572 #else
573   return 0;
574 #endif
575 }
576
577 INLINE int __hscore_vtime( void )
578 {
579 #ifdef VTIME
580   return VTIME;
581 #else
582   return 0;
583 #endif
584 }
585
586 INLINE int __hscore_sigttou( void )
587 {
588 #ifdef SIGTTOU
589   return SIGTTOU;
590 #else
591   return 0;
592 #endif
593 }
594
595 INLINE int __hscore_sig_block( void )
596 {
597 #ifdef SIG_BLOCK
598   return SIG_BLOCK;
599 #else
600   return 0;
601 #endif
602 }
603
604 INLINE int __hscore_sig_setmask( void )
605 {
606 #ifdef SIG_SETMASK
607   return SIG_SETMASK;
608 #else
609   return 0;
610 #endif
611 }
612
613 INLINE int
614 __hscore_f_getfl( void )
615 {
616 #ifdef F_GETFL
617   return F_GETFL;
618 #else
619   return 0;
620 #endif
621 }
622
623 INLINE int
624 __hscore_f_setfl( void )
625 {
626 #ifdef F_SETFL
627   return F_SETFL;
628 #else
629   return 0;
630 #endif
631 }
632
633 // defined in rts/RtsStartup.c.
634 extern void* __hscore_get_saved_termios(int fd);
635 extern void __hscore_set_saved_termios(int fd, void* ts);
636
637 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
638
639 #if !defined(mingw32_TARGET_OS) && !defined(_MSC_VER)
640 INLINE int __hsposix_SIGABRT()   { return SIGABRT; }
641 INLINE int __hsposix_SIGALRM()   { return SIGALRM; }
642 INLINE int __hsposix_SIGBUS()    { return SIGBUS; }
643 INLINE int __hsposix_SIGCHLD()   { return SIGCHLD; }
644 INLINE int __hsposix_SIGCONT()   { return SIGCONT; }
645 INLINE int __hsposix_SIGFPE()    { return SIGFPE; }
646 INLINE int __hsposix_SIGHUP()    { return SIGHUP; }
647 INLINE int __hsposix_SIGILL()    { return SIGILL; }
648 INLINE int __hsposix_SIGINT()    { return SIGINT; }
649 INLINE int __hsposix_SIGKILL()   { return SIGKILL; }
650 INLINE int __hsposix_SIGPIPE()   { return SIGPIPE; }
651 INLINE int __hsposix_SIGQUIT()   { return SIGQUIT; }
652 INLINE int __hsposix_SIGSEGV()   { return SIGSEGV; }
653 INLINE int __hsposix_SIGSTOP()   { return SIGSTOP; }
654 INLINE int __hsposix_SIGTERM()   { return SIGTERM; }
655 INLINE int __hsposix_SIGTSTP()   { return SIGTSTP; }
656 INLINE int __hsposix_SIGTTIN()   { return SIGTTIN; }
657 INLINE int __hsposix_SIGTTOU()   { return SIGTTOU; }
658 INLINE int __hsposix_SIGUSR1()   { return SIGUSR1; }
659 INLINE int __hsposix_SIGUSR2()   { return SIGUSR2; }
660 #if HAVE_SIGPOLL
661 INLINE int __hsposix_SIGPOLL()   { return SIGPOLL; }
662 #endif
663 INLINE int __hsposix_SIGPROF()   { return SIGPROF; }
664 INLINE int __hsposix_SIGSYS()    { return SIGSYS; }
665 INLINE int __hsposix_SIGTRAP()   { return SIGTRAP; }
666 INLINE int __hsposix_SIGURG()    { return SIGURG; }
667 INLINE int __hsposix_SIGVTALRM() { return SIGVTALRM; }
668 INLINE int __hsposix_SIGXCPU()   { return SIGXCPU; }
669 INLINE int __hsposix_SIGXFSZ()   { return SIGXFSZ; }
670
671 INLINE int __hsposix_SIG_BLOCK()   { return SIG_BLOCK; }
672 INLINE int __hsposix_SIG_UNBLOCK() { return SIG_UNBLOCK; }
673 INLINE int __hsposix_SIG_SETMASK() { return SIG_SETMASK; }
674 #endif /* mingw32_TARGET_OS */
675
676 #endif /* __HSBASE_H__ */
677