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