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