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