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