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