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