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