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