[project @ 2002-03-26 23:50:56 by sof]
[ghc-base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsBase.h,v 1.6 2002/03/26 23:50:56 sof 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
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif
28 #ifdef HAVE_FCNTL_H
29 # include <fcntl.h>
30 #endif
31 #ifdef HAVE_TERMIOS_H
32 #include <termios.h>
33 #endif
34 #ifdef HAVE_SIGNAL_H
35 #include <signal.h>
36 #endif
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43 #ifdef HAVE_DIRENT_H
44 #include <dirent.h>
45 #endif
46 #ifdef HAVE_UTIME_H
47 #include <utime.h>
48 #endif
49 #if defined(HAVE_GETTIMEOFDAY)
50 #  ifdef HAVE_SYS_TIME_H
51 #   include <sys/time.h>
52 #  endif
53 #elif defined(HAVE_GETCLOCK)
54 # ifdef HAVE_SYS_TIMERS_H
55 #  define POSIX_4D9 1
56 #  include <sys/timers.h>
57 # endif
58 #endif
59 #if defined(HAVE_TIME_H)
60 # include <time.h>
61 #endif
62 #ifdef HAVE_SYS_TIMEB_H
63 #include <sys/timeb.h>
64 #endif
65 #ifdef HAVE_WINDOWS_H
66 #include <windows.h>
67 #endif
68 #ifdef HAVE_SYS_TIMES_H
69 #include <sys/times.h>
70 #endif
71 #if defined(HAVE_WINSOCK_H) && defined(__MINGW32__)
72 #include <winsock.h>
73 #endif
74 #ifdef HAVE_LIMITS_H
75 #include <limits.h>
76 #endif
77
78 #if !defined(mingw32_TARGET_OS) && !defined(irix_TARGET_OS)
79 # if defined(HAVE_SYS_RESOURCE_H)
80 #  include <sys/resource.h>
81 # endif
82 #endif
83
84 #ifdef hpux_TARGET_OS
85 #include <sys/syscall.h>
86 #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
87 #define HAVE_GETRUSAGE
88 #endif
89
90 /* For System */
91 #ifdef HAVE_SYS_WAIT_H
92 #include <sys/wait.h>
93 #endif
94 #ifdef HAVE_VFORK_H
95 #include <vfork.h>
96 #endif
97 #include "lockFile.h"
98 #include "dirUtils.h"
99 #include "errUtils.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 inputReady.c */
114 int inputReady(int fd, int msecs, int isSock);
115
116 /* -----------------------------------------------------------------------------
117    64-bit operations, defined in longlong.c
118    -------------------------------------------------------------------------- */
119
120 #ifdef SUPPORT_LONG_LONGS
121
122 StgInt stg_gtWord64 (StgWord64, StgWord64);
123 StgInt stg_geWord64 (StgWord64, StgWord64);
124 StgInt stg_eqWord64 (StgWord64, StgWord64);
125 StgInt stg_neWord64 (StgWord64, StgWord64);
126 StgInt stg_ltWord64 (StgWord64, StgWord64);
127 StgInt stg_leWord64 (StgWord64, StgWord64);
128
129 StgInt stg_gtInt64 (StgInt64, StgInt64);
130 StgInt stg_geInt64 (StgInt64, StgInt64);
131 StgInt stg_eqInt64 (StgInt64, StgInt64);
132 StgInt stg_neInt64 (StgInt64, StgInt64);
133 StgInt stg_ltInt64 (StgInt64, StgInt64);
134 StgInt stg_leInt64 (StgInt64, StgInt64);
135
136 StgWord64 stg_remWord64  (StgWord64, StgWord64);
137 StgWord64 stg_quotWord64 (StgWord64, StgWord64);
138
139 StgInt64 stg_remInt64    (StgInt64, StgInt64);
140 StgInt64 stg_quotInt64   (StgInt64, StgInt64);
141 StgInt64 stg_negateInt64 (StgInt64);
142 StgInt64 stg_plusInt64   (StgInt64, StgInt64);
143 StgInt64 stg_minusInt64  (StgInt64, StgInt64);
144 StgInt64 stg_timesInt64  (StgInt64, StgInt64);
145
146 StgWord64 stg_and64  (StgWord64, StgWord64);
147 StgWord64 stg_or64   (StgWord64, StgWord64);
148 StgWord64 stg_xor64  (StgWord64, StgWord64);
149 StgWord64 stg_not64  (StgWord64);
150
151 StgWord64 stg_uncheckedShiftL64   (StgWord64, StgInt);
152 StgWord64 stg_uncheckedShiftRL64  (StgWord64, StgInt);
153 StgInt64  stg_uncheckedIShiftL64  (StgInt64, StgInt);
154 StgInt64  stg_uncheckedIShiftRL64 (StgInt64, StgInt);
155 StgInt64  stg_uncheckedIShiftRA64 (StgInt64, StgInt);
156
157 StgInt64  stg_intToInt64    (StgInt);
158 StgInt    stg_int64ToInt    (StgInt64);
159 StgWord64 stg_int64ToWord64 (StgInt64);
160
161 StgWord64 stg_wordToWord64  (StgWord);
162 StgWord   stg_word64ToWord  (StgWord64);
163 StgInt64  stg_word64ToInt64 (StgWord64);
164
165 StgInt64  stg_integerToInt64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
166 StgWord64 stg_integerToWord64 (StgInt sa, StgByteArray /* Really: mp_limb_t* */ da);
167
168 #endif /* SUPPORT_LONG_LONGS */
169
170 /* -----------------------------------------------------------------------------
171    INLINE functions.
172
173    These functions are given as inlines here for when compiling via C,
174    but we also generate static versions into the cbits library for
175    when compiling to native code.
176    -------------------------------------------------------------------------- */
177
178 #ifndef INLINE
179 #define INLINE extern inline
180 #endif
181
182 INLINE int
183 __hscore_sigaddset( sigset_t * set, int s )
184 { return sigaddset(set,s); }
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 void
197 __hscore_sigemptyset( sigset_t *set )
198 { sigemptyset(set); }
199 #endif
200
201 INLINE void *
202 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
203 { return memcpy(dst+dst_off, src, sz); }
204
205 INLINE void *
206 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
207 { return memcpy(dst, src+src_off, sz); }
208
209 INLINE HsBool
210 __hscore_supportsTextMode()
211 {
212 #if defined(mingw32_TARGET_OS)
213   return HS_BOOL_FALSE;
214 #else
215   return HS_BOOL_TRUE;
216 #endif
217 }
218
219 INLINE HsInt
220 __hscore_bufsiz()
221 {
222   return BUFSIZ;
223 }
224
225 INLINE HsInt
226 __hscore_seek_cur()
227 {
228   return SEEK_CUR;
229 }
230
231 INLINE HsInt
232 __hscore_o_binary()
233 {
234 #ifdef HAVE_O_BINARY
235   return O_BINARY;
236 #else
237   return 0;
238 #endif
239 }
240
241 INLINE int
242 __hscore_o_rdonly()
243 {
244 #ifdef O_RDONLY
245   return O_RDONLY;
246 #else
247   return 0;
248 #endif
249 }
250
251 INLINE int
252 __hscore_o_wronly( void )
253 {
254 #ifdef O_WRONLY
255   return O_WRONLY;
256 #else
257   return 0;
258 #endif
259 }
260
261 INLINE int
262 __hscore_o_rdwr( void )
263 {
264 #ifdef O_RDWR
265   return O_RDWR;
266 #else
267   return 0;
268 #endif
269 }
270
271 INLINE int
272 __hscore_o_append( void )
273 {
274 #ifdef O_APPEND
275   return O_APPEND;
276 #else
277   return 0;
278 #endif
279 }
280
281 INLINE int
282 __hscore_o_creat( void )
283 {
284 #ifdef O_CREAT
285   return O_CREAT;
286 #else
287   return 0;
288 #endif
289 }
290
291 INLINE int
292 __hscore_o_excl( void )
293 {
294 #ifdef O_EXCL
295   return O_EXCL;
296 #else
297   return 0;
298 #endif
299 }
300
301 INLINE int
302 __hscore_o_trunc( void )
303 {
304 #ifdef O_TRUNC
305   return O_TRUNC;
306 #else
307   return 0;
308 #endif
309 }
310
311 INLINE int
312 __hscore_o_noctty( void )
313 {
314 #ifdef O_NOCTTY
315   return O_NOCTTY;
316 #else
317   return 0;
318 #endif
319 }
320
321 INLINE int
322 __hscore_o_nonblock( void )
323 {
324 #ifdef O_NONBLOCK
325   return O_NONBLOCK;
326 #else
327   return 0;
328 #endif
329 }
330
331 INLINE HsInt
332 __hscore_seek_set( void )
333 {
334   return SEEK_SET;
335 }
336
337 INLINE HsInt
338 __hscore_seek_end( void )
339 {
340   return SEEK_END;
341 }
342
343 INLINE HsInt
344 __hscore_setmode( HsInt fd, HsBool toBin )
345 {
346 #if defined(__MINGW32__)
347   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
348 #else
349   return 0;
350 #endif  
351 }
352
353 INLINE HsInt
354 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
355                            HsInt off, int sz )
356 {
357 #if defined(__MINGW32__)
358   if (isSock) {
359     return send(fd,ptr + off, sz, 0);
360   }
361 #endif
362   return write(fd,ptr + off, sz);
363 }
364
365 INLINE HsInt
366 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
367                           HsInt off, int sz )
368 {
369 #if defined(__MINGW32__)
370   if (isSock) {
371     return recv(fd,ptr + off, sz, 0);
372   }
373 #endif
374   return read(fd,ptr + off, sz);
375
376 }
377
378 #if defined(__MINGW32__)
379 INLINE long *
380 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
381
382 INLINE char **
383 __hscore_Time_ghcTzname( void ) { return _tzname; }
384 #endif
385
386 INLINE HsInt
387 __hscore_mkdir( HsAddr pathName, HsInt mode )
388 {
389 #if defined(__MINGW32__)
390   return mkdir(pathName);
391 #else
392   return mkdir(pathName,mode);
393 #endif
394 }
395
396 INLINE HsInt
397 __hscore_lstat( HsAddr fname, HsAddr st )
398 {
399 #ifdef HAVE_LSTAT
400   return lstat((const char*)fname, (struct stat*)st);
401 #else
402   return stat((const char*)fname, (struct stat*)st);
403 #endif
404 }
405
406 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
407
408 INLINE mode_t __hscore_R_OK() { return R_OK; }
409 INLINE mode_t __hscore_W_OK() { return W_OK; }
410 INLINE mode_t __hscore_X_OK() { return X_OK; }
411
412 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
413 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
414 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
415
416 INLINE HsAddr
417 __hscore_d_name( struct dirent* d )
418
419 #ifndef mingw32_TARGET_OS
420   return (HsAddr)(&d->d_name);
421 #else
422   return (HsAddr)(d->d_name);
423 #endif
424 }
425
426 INLINE HsInt
427 __hscore_end_of_dir( void )
428 {
429 #ifndef mingw32_TARGET_OS
430   return 0;
431 #else
432   return ENOENT;
433 #endif  
434 }
435
436 INLINE void
437 __hscore_free_dirent(HsAddr dEnt)
438 {
439 #if HAVE_READDIR_R
440   free(dEnt);
441 #endif
442 }
443
444 INLINE HsInt
445 __hscore_sizeof_stat( void )
446 {
447   return sizeof(struct stat);
448 }
449
450 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
451 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
452 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
453
454 #if HAVE_TERMIOS_H
455 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
456
457 INLINE void
458 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
459
460 INLINE unsigned char*
461 __hscore_ptr_c_cc( struct termios* ts ) 
462 { return (unsigned char*) &ts->c_cc; }
463 #endif
464
465 INLINE HsInt
466 __hscore_sizeof_termios( void )
467 {
468 #ifndef mingw32_TARGET_OS
469   return sizeof(struct termios);
470 #else
471   return 0;
472 #endif
473 }
474
475 INLINE HsInt
476 __hscore_sizeof_sigset_t( void )
477 {
478 #ifndef mingw32_TARGET_OS
479   return sizeof(sigset_t);
480 #else
481   return 0;
482 #endif
483 }
484
485 INLINE int
486 __hscore_echo( void )
487 {
488 #ifdef ECHO
489   return ECHO;
490 #else
491   return 0;
492 #endif
493
494 }
495
496 INLINE int
497 __hscore_tcsanow( void )
498 {
499 #ifdef TCSANOW
500   return TCSANOW;
501 #else
502   return 0;
503 #endif
504
505 }
506
507 INLINE int
508 __hscore_icanon( void )
509 {
510 #ifdef ICANON
511   return ICANON;
512 #else
513   return 0;
514 #endif
515 }
516
517 INLINE int __hscore_vmin( void )
518 {
519 #ifdef VMIN
520   return VMIN;
521 #else
522   return 0;
523 #endif
524 }
525
526 INLINE int __hscore_vtime( void )
527 {
528 #ifdef VTIME
529   return VTIME;
530 #else
531   return 0;
532 #endif
533 }
534
535 INLINE int __hscore_sigttou( void )
536 {
537 #ifdef SIGTTOU
538   return SIGTTOU;
539 #else
540   return 0;
541 #endif
542 }
543
544 INLINE int __hscore_sig_block( void )
545 {
546 #ifdef SIG_BLOCK
547   return SIG_BLOCK;
548 #else
549   return 0;
550 #endif
551 }
552
553 INLINE int __hscore_sig_setmask( void )
554 {
555 #ifdef SIG_SETMASK
556   return SIG_SETMASK;
557 #else
558   return 0;
559 #endif
560 }
561
562 INLINE int
563 __hscore_f_getfl( void )
564 {
565 #ifdef F_GETFL
566   return F_GETFL;
567 #else
568   return 0;
569 #endif
570 }
571
572 INLINE int
573 __hscore_f_setfl( void )
574 {
575 #ifdef F_SETFL
576   return F_SETFL;
577 #else
578   return 0;
579 #endif
580 }
581
582 #endif
583