[project @ 2002-07-04 13:33:24 by simonmar]
[haskell-directory.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2  * $Id: HsBase.h,v 1.9 2002/07/04 13:33:25 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
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 #if !defined(mingw32_TARGET_OS)
183 INLINE int
184 __hscore_sigaddset( sigset_t * set, int s )
185 { return sigaddset(set,s); }
186 #endif
187
188 INLINE int __hscore_s_isreg(m)  { return S_ISREG(m);  }
189 INLINE int __hscore_s_isdir(m)  { return S_ISDIR(m);  }
190 INLINE int __hscore_s_isfifo(m) { return S_ISFIFO(m); }
191 INLINE int __hscore_s_isblk(m)  { return S_ISBLK(m);  }
192 INLINE int __hscore_s_ischr(m)  { return S_ISCHR(m);  }
193 #ifdef S_ISSOCK
194 INLINE int __hscore_s_issock(m) { return S_ISSOCK(m); }
195 #endif
196
197 #ifndef mingw32_TARGET_OS
198 INLINE void
199 __hscore_sigemptyset( sigset_t *set )
200 { sigemptyset(set); }
201 #endif
202
203 INLINE void *
204 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
205 { return memcpy(dst+dst_off, src, sz); }
206
207 INLINE void *
208 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
209 { return memcpy(dst, src+src_off, sz); }
210
211 INLINE HsBool
212 __hscore_supportsTextMode()
213 {
214 #if defined(mingw32_TARGET_OS)
215   return HS_BOOL_FALSE;
216 #else
217   return HS_BOOL_TRUE;
218 #endif
219 }
220
221 INLINE HsInt
222 __hscore_bufsiz()
223 {
224   return BUFSIZ;
225 }
226
227 INLINE HsInt
228 __hscore_seek_cur()
229 {
230   return SEEK_CUR;
231 }
232
233 INLINE HsInt
234 __hscore_o_binary()
235 {
236 #ifdef HAVE_O_BINARY
237   return O_BINARY;
238 #else
239   return 0;
240 #endif
241 }
242
243 INLINE int
244 __hscore_o_rdonly()
245 {
246 #ifdef O_RDONLY
247   return O_RDONLY;
248 #else
249   return 0;
250 #endif
251 }
252
253 INLINE int
254 __hscore_o_wronly( void )
255 {
256 #ifdef O_WRONLY
257   return O_WRONLY;
258 #else
259   return 0;
260 #endif
261 }
262
263 INLINE int
264 __hscore_o_rdwr( void )
265 {
266 #ifdef O_RDWR
267   return O_RDWR;
268 #else
269   return 0;
270 #endif
271 }
272
273 INLINE int
274 __hscore_o_append( void )
275 {
276 #ifdef O_APPEND
277   return O_APPEND;
278 #else
279   return 0;
280 #endif
281 }
282
283 INLINE int
284 __hscore_o_creat( void )
285 {
286 #ifdef O_CREAT
287   return O_CREAT;
288 #else
289   return 0;
290 #endif
291 }
292
293 INLINE int
294 __hscore_o_excl( void )
295 {
296 #ifdef O_EXCL
297   return O_EXCL;
298 #else
299   return 0;
300 #endif
301 }
302
303 INLINE int
304 __hscore_o_trunc( void )
305 {
306 #ifdef O_TRUNC
307   return O_TRUNC;
308 #else
309   return 0;
310 #endif
311 }
312
313 INLINE int
314 __hscore_o_noctty( void )
315 {
316 #ifdef O_NOCTTY
317   return O_NOCTTY;
318 #else
319   return 0;
320 #endif
321 }
322
323 INLINE int
324 __hscore_o_nonblock( void )
325 {
326 #ifdef O_NONBLOCK
327   return O_NONBLOCK;
328 #else
329   return 0;
330 #endif
331 }
332
333 INLINE HsInt
334 __hscore_seek_set( void )
335 {
336   return SEEK_SET;
337 }
338
339 INLINE HsInt
340 __hscore_seek_end( void )
341 {
342   return SEEK_END;
343 }
344
345 INLINE HsInt
346 __hscore_setmode( HsInt fd, HsBool toBin )
347 {
348 #if defined(__MINGW32__)
349   return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
350 #else
351   return 0;
352 #endif  
353 }
354
355 INLINE HsInt
356 __hscore_PrelHandle_write( HsInt fd, HsBool isSock, HsAddr ptr, 
357                            HsInt off, int sz )
358 {
359 #if defined(__MINGW32__)
360   if (isSock) {
361     return send(fd,ptr + off, sz, 0);
362   }
363 #endif
364   return write(fd,ptr + off, sz);
365 }
366
367 INLINE HsInt
368 __hscore_PrelHandle_read( HsInt fd, HsBool isSock, HsAddr ptr, 
369                           HsInt off, int sz )
370 {
371 #if defined(__MINGW32__)
372   if (isSock) {
373     return recv(fd,ptr + off, sz, 0);
374   }
375 #endif
376   return read(fd,ptr + off, sz);
377
378 }
379
380 #if defined(__MINGW32__)
381 INLINE long *
382 __hscore_Time_ghcTimezone( void ) { return &_timezone; }
383
384 INLINE char **
385 __hscore_Time_ghcTzname( void ) { return _tzname; }
386 #endif
387
388 INLINE HsInt
389 __hscore_mkdir( HsAddr pathName, HsInt mode )
390 {
391 #if defined(__MINGW32__)
392   return mkdir(pathName);
393 #else
394   return mkdir(pathName,mode);
395 #endif
396 }
397
398 INLINE HsInt
399 __hscore_lstat( HsAddr fname, HsAddr st )
400 {
401 #ifdef HAVE_LSTAT
402   return lstat((const char*)fname, (struct stat*)st);
403 #else
404   return stat((const char*)fname, (struct stat*)st);
405 #endif
406 }
407
408 INLINE HsInt __hscore_path_max() { return PATH_MAX; }
409
410 INLINE mode_t __hscore_R_OK() { return R_OK; }
411 INLINE mode_t __hscore_W_OK() { return W_OK; }
412 INLINE mode_t __hscore_X_OK() { return X_OK; }
413
414 INLINE mode_t __hscore_S_IRUSR() { return S_IRUSR; }
415 INLINE mode_t __hscore_S_IWUSR() { return S_IWUSR; }
416 INLINE mode_t __hscore_S_IXUSR() { return S_IXUSR; }
417
418 INLINE HsAddr
419 __hscore_d_name( struct dirent* d )
420
421 #ifndef mingw32_TARGET_OS
422   return (HsAddr)(&d->d_name);
423 #else
424   return (HsAddr)(d->d_name);
425 #endif
426 }
427
428 INLINE HsInt
429 __hscore_end_of_dir( void )
430 {
431 #ifndef mingw32_TARGET_OS
432   return 0;
433 #else
434   return ENOENT;
435 #endif  
436 }
437
438 INLINE void
439 __hscore_free_dirent(HsAddr dEnt)
440 {
441 #if HAVE_READDIR_R
442   free(dEnt);
443 #endif
444 }
445
446 INLINE HsInt
447 __hscore_sizeof_stat( void )
448 {
449   return sizeof(struct stat);
450 }
451
452 INLINE time_t __hscore_st_mtime ( struct stat* st ) { return st->st_mtime; }
453 INLINE off_t  __hscore_st_size  ( struct stat* st ) { return st->st_size; }
454 INLINE mode_t __hscore_st_mode  ( struct stat* st ) { return st->st_mode; }
455
456 #if HAVE_TERMIOS_H
457 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
458
459 INLINE void
460 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
461
462 INLINE unsigned char*
463 __hscore_ptr_c_cc( struct termios* ts ) 
464 { return (unsigned char*) &ts->c_cc; }
465 #endif
466
467 INLINE HsInt
468 __hscore_sizeof_termios( void )
469 {
470 #ifndef mingw32_TARGET_OS
471   return sizeof(struct termios);
472 #else
473   return 0;
474 #endif
475 }
476
477 INLINE HsInt
478 __hscore_sizeof_sigset_t( void )
479 {
480 #ifndef mingw32_TARGET_OS
481   return sizeof(sigset_t);
482 #else
483   return 0;
484 #endif
485 }
486
487 INLINE int
488 __hscore_echo( void )
489 {
490 #ifdef ECHO
491   return ECHO;
492 #else
493   return 0;
494 #endif
495
496 }
497
498 INLINE int
499 __hscore_tcsanow( void )
500 {
501 #ifdef TCSANOW
502   return TCSANOW;
503 #else
504   return 0;
505 #endif
506
507 }
508
509 INLINE int
510 __hscore_icanon( void )
511 {
512 #ifdef ICANON
513   return ICANON;
514 #else
515   return 0;
516 #endif
517 }
518
519 INLINE int __hscore_vmin( void )
520 {
521 #ifdef VMIN
522   return VMIN;
523 #else
524   return 0;
525 #endif
526 }
527
528 INLINE int __hscore_vtime( void )
529 {
530 #ifdef VTIME
531   return VTIME;
532 #else
533   return 0;
534 #endif
535 }
536
537 INLINE int __hscore_sigttou( void )
538 {
539 #ifdef SIGTTOU
540   return SIGTTOU;
541 #else
542   return 0;
543 #endif
544 }
545
546 INLINE int __hscore_sig_block( void )
547 {
548 #ifdef SIG_BLOCK
549   return SIG_BLOCK;
550 #else
551   return 0;
552 #endif
553 }
554
555 INLINE int __hscore_sig_setmask( void )
556 {
557 #ifdef SIG_SETMASK
558   return SIG_SETMASK;
559 #else
560   return 0;
561 #endif
562 }
563
564 INLINE int
565 __hscore_f_getfl( void )
566 {
567 #ifdef F_GETFL
568   return F_GETFL;
569 #else
570   return 0;
571 #endif
572 }
573
574 INLINE int
575 __hscore_f_setfl( void )
576 {
577 #ifdef F_SETFL
578   return F_SETFL;
579 #else
580   return 0;
581 #endif
582 }
583
584 #endif
585