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