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